Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
SQL
Declare @QryMax nvarchar(max)
Declare @RangeFrom VARCHAR(MAX),
Declare @RangeTo VARCHAR(MAX),
Declare @RangeFilter VARCHAR(MAX),
Declare @OneMB int = 1024*1024

SQL
set @QryMax = 'select top (1) MAX( SizePart) Data
            from (
                    select DATEPART('+ @RangeFilter+ ',CreatedOn) as RangeFilter , (FileSize/('+CONVERT (VARCHAR(10), @OneMB)+')) as SizePart from TblAssetDetails
                        where IsDeleted = ''False'' and CreatedOn >= '''+@RangeFrom+''' and CreatedOn <= '''+@RangeTo +'''
                 ) as tbl
            group by RangeFilter'
exec @QryMax


i want to set result of @QryMax (it will be int value) in a variable and

SQL
select * from Get_DataUnit_WithDataDivider(@MaxData)


this function will give in unit in MB, GB, TB and divider, for further data processing.
Posted
Updated 27-Sep-15 18:56pm
v2

1 solution

In TSQL, you could try using sp_executesql.

sp_executesql: https://msdn.microsoft.com/en-GB/library/ms188001.aspx[^]

Something like this:
SQL
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);--note changed from @MaxData in your question

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900