1
我想在PIVOT中動態地傳遞值。請檢查下面的代碼,我得到了錯誤 - 必須聲明標量變量「@empid」。請幫我...在SQL透視中添加動態值
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 [email protected] 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)