SQL 将存储过程的结果保存在表变量中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4595500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save result of stored procedure in a Table variable
提问by Jan
Possible Duplicate:
How to SELECT * INTO [temp table] FROM [stored procedure]
I have a nested stored procedure call
我有一个嵌套的存储过程调用
In one of the stored procedures I want to save the result into a table variable likt this :
在其中一个存储过程中,我想将结果保存到一个表变量中,如下所示:
INSERT INTO @myTable
EXEC sp_myStoredProcedure
however, because the proc. is nested the following error occurs : An INSERT EXEC statement cannot be nested
但是,由于 proc。嵌套时发生以下错误:不能嵌套 INSERT EXEC 语句
The procedure must be called from another procedure, changing this is not an option. I wanted to try to use an output parameter but it still has to be set with a Insert into statement.
该过程必须从另一个过程调用,改变它不是一个选项。我想尝试使用输出参数,但它仍然必须使用 Insert into 语句进行设置。
What are other options to save the data that is retrieved from the call of a Stored Procedure into a variable ?
将调用存储过程检索到的数据保存到变量中的其他选项是什么?
回答by Jahan
Table variables are not visible to the calling procedure in the case of nested procs. The following is legal with #temp tables.
在嵌套过程的情况下,表变量对调用过程不可见。以下对#temp 表是合法的。
http://databases.aspfaq.com/database/should-i-use-a-temp-table-or-a-table-variable.html
http://databases.aspfaq.com/database/should-i-use-a-temp-table-or-a-table-variable.html
回答by wergeld
Not tested but maybe do this:
未经测试,但可以这样做:
DECLARE @insertedProc as varchar(300)
SET @insertedProc = 'INSERT INTO ' + @myTable + ' sp_myStoredProcedure'
EXEC @insertedProc
Make sure you have defined what @myTable is before hand.
确保您事先定义了@myTable 是什么。