sql server - Pivot with a table variable -
i'm unable use pivot data of table variable.
its giving following error on run-time: "must declare scalar variable @reportdata"
i have tried mentioned below
declare @reportdata table ( personid numeric(6,0), personname varchar(100), bu varchar(50), timeperiod varchar(100), wfstatus varchar(100) ) i'm using below dynamic pivot query
declare @query nvarchar(max) set @query=n'select personid,personname,bu,wfstatus,'+@datelist+' from( select personid,personname,bu,wfstatus,timeperiod '+@reportdata+') sourcetable pivot (group wfstatus timeperiod in('+@datelist+') ) pivorttable select personid,personname,bu,timeperiod,status pivorttable' execute(@query); can 1 me in this? need use table variable maintain concurrency issue.!
from'+@reportdata attempts add table variable string, wont work table variable not string.
given presumably need populate reportdata first switch explicitly created temp table
create table #reportdata ( personid numeric(6,0) ... ) or use table type;
--run once create type reportdatatype table ( personid numeric(6,0), personname varchar(100) ) declare @reportdata reportdatatype insert @reportdata values (111, 'bob'), (222, 'alice') declare @query nvarchar(max) = n'select * @t' exec sp_executesql @query, n'@t reportdatatype readonly', @reportdata
Comments
Post a Comment