在存储过程中使用pivot表。

时间:2021-01-01 10:09:05

I have a pivot query that works great!

我有一个非常好用的pivot查询!

;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

Now I want to use that query as the source of a temp table in a stored procedure and none of the syntax that I have tried to INSERT the results of the query into a temp table have worked.

现在,我希望将该查询用作存储过程中的临时表的源,而不是我试图将查询结果插入到临时表中的语法。

Can I create a temporary table from the results? If so, how?

我可以从结果中创建一个临时表吗?如果是这样,如何?

Thanks!

谢谢!

Leslie

莱斯利

1 个解决方案

#1


0  

Please try this :

请试试这个:

;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

here you dont have to declare a table or table variable specifically for the purpose.

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

#1


0  

Please try this :

请试试这个:

;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

here you dont have to declare a table or table variable specifically for the purpose.

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