Error in executing dynamic SQL Query with table variable in SQL Server 2008 -
these following parameters & table variable used executing dynamic sql query:
declaration
declare @squery varchar(max) declare @fyear nvarchar(10)='2016-2017' declare @tyear nvarchar(10)='2016-2018' declare @claimsum table ( claimtype nvarchar(max), jan decimal(19,6), feb decimal(19,6), mar decimal(19,6), apr decimal(19,6), may decimal(19,6), jun decimal(19,6), jul decimal(19,6), aug decimal(19,6), sep decimal(19,6), oct decimal(19,6), nov decimal(19,6), dec decimal(19,6), total decimal(19,6) )
dynamic sql query
set @squery=n'select c1.claimtype, sum(c1.jan) ''jan '+@fyear+'-'+@tyear+''',sum(c1.feb) ''feb '+@fyear+'-'+@tyear+''',sum(c1.mar) ''mar '+@fyear+'-'+@tyear+''', sum(c1.apr) ''apr '+@fyear+'-'+@tyear+''',sum(c1.may) ''may '+@fyear+'-'+@tyear+''',sum(c1.jun) ''jun '+@fyear+'-'+@tyear+''', sum(c1.jul) ''jul '+@fyear+'-'+@tyear+''',sum(c1.aug) ''aug '+@fyear+'-'+@tyear+''',sum(c1.sep) ''sep '+@fyear+'-'+@tyear+''', sum(c1.oct) ''oct '+@fyear+'-'+@tyear+''',sum(c1.nov) ''nov '+@fyear+'-'+@tyear+''',sum(c1.dec) ''dec '+@fyear+'-'+@tyear+''', sum(c1.total) ''total'' @claimsum c1 group c1.claimtype'; execute @squery
error when executing query getting following error:
the name 'select c1.claimtype, sum(c1.jan) 'jan 2016-2017',sum(c1.feb) 'feb 2016-2017',sum(c1.mar) 'mar 2016-2017', sum(c1.apr) 'apr 2016-2017',sum(c1.may) 'may 2016-2017',sum(c1.jun) 'jun 2016-2017', sum(c1.jul) 'jul 2016-2017',sum(c1.aug) 'aug 2016-2017',sum(c1.sep) 'sep 2016-2017', sum(c1.oct) 'oct 2016-2017',sum(c1.nov) 'nov 2016-2017',sum(c1.dec) 'dec 2016-2017', sum(c1.total) 'total' @claimsum c1 group c1.claimtype' not valid identifier.
i had replaced execute @squery
execute (@squery)
, working fine.
Comments
Post a Comment