根据来自 SP 的结果集动态创建临时表

编程入门 行业动态 更新时间:2024-10-27 13:32:01
本文介绍了根据来自 SP 的结果集动态创建临时表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个调用另一个 SP 的 SP,结果集需要过滤到我感兴趣的列.

I have a SP that calls another SP and the resultset needs to be filtered to only the columns that I am interested in.

DECLARE @myTable TABLE ( Field1 INT, Field2 INT, Field3 INT ) --If someSP returns say 30 params and I need only 3 params, I don't want to declare all 30 in my temp table @myTable INSERT INTO @myTable (Field1, Field2, Field3) EXEC someSP --Need to selectively filter recordset from SP @InputParam1 = 'test'

如果我不能这样做,我想根据 someSP 的结果集动态创建临时表(这样可以在修改 someSP 以添加新参数时减轻维护问题,我也不需要修改此过程

If I cannot do this, I would want to create the temp table DYNAMICALLY based on the resultset from someSP (This way it relieves maintenance issues when someSP is modified to add a new param, I dont need to modify this proc as well

推荐答案

简短回答:不,你不能那样做.

Short answer: no, you can't do that.

您必须使用将从存储过程返回的确切列数预先声明您的临时表.

You have to pre-declare your temp table with the exact number of columns that will be returned from the stored proc.

解决方法是使用持久表.例如,您的数据库中可以有一个名为 someSPResults 的永久表.每当 someSP 更改为具有不同数量的输出列时,请在部署过程中更改 someSPResults 的格式.

The workaround is to use persistent tables. For example, you could have a permanent table in your database called someSPResults. Whenever someSP is changed to have a different number of output columns, change the format of someSPResults as part of the deployment.

然后你可以这样做:

insert into dbo.someSPresults exec someSP

或者在 someSP 内部,您可以将结果直接插入到 someSPresults 表中,作为执行的正常部分.您只需要确保准确识别 someSPresults 表中的哪些记录来自 someSP 的每次执行,因为存储的 proc 可能同时被触发多次,从而将大量数据转储到 someSPresults 中.

Or inside someSP, you can have the results be inserted directly into the someSPresults table as a normal part of execution. You just have to make sure to identify exactly which records in the someSPresults table came from each execution of someSP, because that stored proc could be fired multiple times simultaneously, thereby dumping a lot of data into someSPresults.

更多推荐

根据来自 SP 的结果集动态创建临时表

本文发布于:2023-07-22 23:35:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1192054.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动态   SP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!