In TSQL, you could try using sp_executesql.
sp_executesql:
https://msdn.microsoft.com/en-GB/library/ms188001.aspx[
^]
Something like this:
declare @QryMax nvarchar(max);
declare @SQLString nvarchar(max);
declare @ParmDefinition nvarchar(1000);
set @SQLString = N'select top (1) @QryMax = MAX( SizePart) Data from ...'
set @ParmDefinition = N'@max_OUT int OUTPUT';
exec sp_executesql @SQLString, @ParmDefinition, @max_OUT = @QryMax Output;
select * from Get_DataUnit_WithDataDivider(@QryMax);
That should help you out, following the link to see how to send in parameters.
Also test to see if your dynamic sql is returning any records, as you may also be having issues with the group by.