Pivot the results of a stored procedure?

空扰寡人 提交于 2019-12-23 05:18:27

问题


I have a stored procedure that always returns one row. I want to convert each column_name and its corresponding value to a row. Example -

ID | Name | Address
-----------------------
1  | Jim  | Home

should become -

ID      |  1
---------------------
Name    | Jim
Address | Home

How do I do this ?


回答1:


You'll need to insert the values from the stored procedure into a (temporary) table, and then unpivot from there. Psuedocode below:

CREATE TABLE #t (ID int, Name varchar(100), Address varchar(100))

INSERT INTO #t
EXEC stored_proc

SELECT ID = 'Name', [1] = Name
FROM #t
UNION ALL
SELECT ID = 'Address', [1] = Address
FROM #t

DROP TABLE #t


来源:https://stackoverflow.com/questions/20960784/pivot-the-results-of-a-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!