1

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)
4

1 に答える 1