0

テーブル変数のデータのピボットを使用できません。

実行時に次のエラーが発生します:「スカラー変数 @reportData を宣言する必要があります」

私は以下のように試しました

DECLARE @reportData TABLE
(
PERSONID NUMERIC(6,0),
personname VARCHAR(100),
bu VARCHAR(50),
timeperiod VARCHAR(100),
wfstatus VARCHAR(100)
)

以下の動的ピボットクエリを使用しています

declare @query nvarchar(max)
set @query=N'SELECT PERSONID,PERSONNAME,BU,wfstatus,'+@datelist+'
from(
SELECT PERSONID,PERSONNAME,BU,wfstatus,timeperiod
FROM
'+@reportData+') AS SOURCETABLE
PIVOT
(group by wfstatus 
FOR timeperiod
 IN('+@datelist+')
) as pivorttable
select personid,personname,bu,timeperiod,status from pivorttable'

execute(@query);

誰かがこれで私を助けることができますか? 同時実行の問題を維持するには、テーブル変数のみを使用する必要があります。

4

1 に答える 1

1

FROM'+@reportData attempts to add a table variable to a string, which wont work as a table variable is not a string.

Given that you presumably need to populate reportData first you could switch to an explicitly created temp table

create table #reportData 
(
PERSONID NUMERIC(6,0)
...
)

Or use a Table Type;

--run once
CREATE TYPE ReportDataType AS TABLE (
    PERSONID NUMERIC(6,0),
    personname VARCHAR(100)
)


declare @reportData ReportDataType
insert @reportData values 
    (111, 'bob'),
    (222, 'alice')

declare @query nvarchar(max) = N'select * from @T'

exec sp_executesql @query, N'@T ReportDataType readonly', @reportData
于 2013-05-15T12:14:13.210 に答える