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] FROM PivotData
PIVOT (Max(PivotData.FieldValue) for ColumnName In ([List of Fields])) As P

现在我想将该查询用作存储过程中临时表的源,并且我尝试将查询结果插入临时表的语法都不起作用。

我可以根据结果创建一个临时表吗?如果是这样,怎么做?

谢谢!

莱斯利

4

1 回答 1

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

在这里,您不必专门为此目的声明表或表变量。

于 2013-07-29T14:12:30.983 回答