简体   繁体   English

透视存储过程的结果?

[英]Pivot the results of a stored procedure?

I have a stored procedure that always returns one row. 我有一个存储过程,总是返回一行。 I want to convert each column_name and its corresponding value to a row. 我想将每个column_name及其对应的值转换为一行。 Example - 范例-

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

should become - 应该变成-

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

How do I do this ? 我该怎么做呢 ?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM