开发者

Dynamic Stored Procedure Results to Table

开发者 https://www.devze.com 2023-01-17 17:12 出处:网络
I have a stored procedure that dynamically produces pivot results, passing sql for row defs, column to pivot, aggregate(field) to sum, and table name of aggregate.This works great, but i need开发者_JA

I have a stored procedure that dynamically produces pivot results, passing sql for row defs, column to pivot, aggregate(field) to sum, and table name of aggregate. This works great, but i need开发者_JAVA百科 to produce a table from these results to use in further calculations.

How can I dynamically save the results to a table within the stored procedure (temp or non temp) without knowing the output columns??


SELECT *
INTO #TempTable
FROM (Pivot Expression)

This will create a #TempTable with the results of whatever you have in the FROM clause, regardless of number/type of columns.


you didn't ask, but this is how I'm getting a set of column names from a view:

DECLARE @columns VARCHAR(1000)

SELECT @columns = COALESCE(@columns + ',[' + cast(fld as varchar) + ']',
'[' + cast(fld as varchar)+ ']')
FROM view
GROUP BY fld
0

精彩评论

暂无评论...
验证码 换一张
取 消