我想在 PIVOT 中动态传递值。请查看以下代码,我得到了错误 - 必须声明标量变量“@empid”。请帮我...
declare @empid nvarchar(20), @fromdate date, @todate date, @cols nvarchar(max), @query nvarchar(max)
set @empid = 'EC0100'
set @fromdate = '10/01/13'
set @todate = '10/31/13'
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(c._Date)
FROM MIS_BM_Calendar c
where c._Date between @fromdate and @todate
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'select * from
(
select e.UserName, c._Date ,SUM(DATEDIFF(SS,0,c.TimeTaken)) As TimeTaken from MIS_BM_Users e
inner join MIS_Opus c on
e.EmpId=c.EmpId
where e.TeamLeaderID=@empid and c._Date between @fromdate and @todate
group by c._Date, e.UserName
) As SourceTable
Pivot
(
SUM(TimeTaken) for _Date In ('+ @cols +') As Pvt'
execute(@query)