透视存储过程的结果? [英] Pivot the results of a stored procedure?

查看:28
本文介绍了透视存储过程的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个总是返回一行的存储过程.我想将每个 column_name 及其对应的值转换为一行.示例 -

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

应该变成 -

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

我该怎么做?

推荐答案

您需要将存储过程中的值插入到(临时)表中,然后从那里逆透视.伪代码如下:

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

这篇关于透视存储过程的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆