我有一个非常有效的数据透视查询!
;with PivotData As (
Select KeyField, ColumnName, FieldValue FROM
(SELECT SomeField as KeyField, NameField As ColumnName, DataField as FieldValue
FROM Table1
UNION
SELECT SomeField, NameField, DataField
FROM Table2)
SELECT [List of Fields] FROM PivotData
PIVOT (Max(PivotData.FieldValue) for ColumnName In ([List of Fields])) As P
现在我想将该查询用作存储过程中临时表的源,并且我试图将查询结果插入临时表的语法都没有用。
我可以从结果中创建临时表吗?如果是这样,怎么样?
谢谢!
莱斯利
答案 0 :(得分:0)
请试试这个:
;with PivotData As (
Select KeyField, ColumnName, FieldValue FROM
(SELECT SomeField as KeyField, NameField As ColumnName, DataField as FieldValue
FROM Table1
UNION
SELECT SomeField, NameField, DataField
FROM Table2)
SELECT [List of Fields] into #temp FROM PivotData
PIVOT (Max(PivotData.FieldValue) for ColumnName In ([List of Fields])) As P
这里你不必专门为此目的声明一个表或表变量。