如何将存储过程的结果(列表动态)转换为sql server中的表变量(Insert) [英] How to take a result of stored procedure(Column list Dynamic) into table varible(Insert) in sql server

查看:170
本文介绍了如何将存储过程的结果(列表动态)转换为sql server中的表变量(Insert)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个存储过程会根据传递的参数返回动态列。

例如

如果参数传递了Branchcode = 0999然后在我的选择列表中

列将是clientCode,ClientName,BranchCode



类似如果传递参数为Age,Gender

列将是clientCode,ClientName,BranchCode,年龄,性别

....

列数是可变的。



i希望上面的存储过程结果变成表变量

,之后基于行数。我必须进行另一项操作。



请帮助

谢谢

解决方案

我建议在所有情况下从存储过程返回完全相同的结构,只为那个特定情况下您不感兴趣的列放置NULL值...这将使您能够使用单个表来存储结果和继续...

 创建   @ myTable 
- your your字段...


INSERT INTO @ myTable
EXEC mySP( @ param


Hi
I am Having one Stored Procedure which will return Dynamic Column based on Parameter passed.
for example
if Parameter passed Branchcode=0999 then in my select list
column will be clientCode, ClientName,BranchCode

similarly if passed parameter as Age,Gender
column will be clientCode, ClientName,BranchCode,Age,Gender
....
number of columns is variable.

i want above stored procedure result into table varible
and after that based on number of rows. i have to perform another operation.

please help
thanks

解决方案

I suggest that in all cases return the exact same structure from the stored procedure, only put NULL values for the columns you are not interested in for that specific case...That will enable you to use a single table to store the result and go forward with...

CREATE TABLE @myTable (
 -- your fields...
)

INSERT INTO @myTable
EXEC mySP (@param)


这篇关于如何将存储过程的结果(列表动态)转换为sql server中的表变量(Insert)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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