简体   繁体   English

从存储过程中调用存储过程并返回记录集

[英]Calling a Stored Proc from within a Stored Proc and returning a recordset

I have a Stored Procedure that rolls-back a series of operations. 我有一个存储过程,可以回滚一系列操作。 I want to call this from within another SP. 我想从另一个SP中调用它。

The problem is that the inner SP returns a record set with a single value that indicates the degree of success. 问题是内部SP返回一个记录集,其中包含一个指示成功程度的值。

This approach worked well and has some advantages in our context, but in retrospect, I would have done it the conventional way with a Return value or an Output parameter. 这种方法效果很好,并且在我们的上下文中有一些优点,但回想起来,我会用传统的方式使用Return值或Output参数。

I could always change this SP to use this approach and modify the calling code, but a) I don't want to dabble with any more code than I have to, and b) at an intellectual level, I'm curious to see what alternative solution there may be, if any. 我总是可以改变这个SP来使用这种方法并修改调用代码,但是a)我不想涉及任何比我更多的代码,并且b)在知识层面,我很想知道什么可能存在替代解决方案,如果有的话。

How (if at all) can I call this SP and determine the value of the singleton recordset returned? 如何(如果有的话)我可以调用此SP并确定返回的单例记录集的值?

Thanks 谢谢

A stored procedure returns a record set like any other, so you can actually do this: 存储过程返回与任何其他记录集一样的记录集,因此您实际上可以执行此操作:

INSERT INTO MyTable ( MyValue ) INSERT INTO MyTable(MyValue)

EXEC dbo.MyStoredProcedure EXEC dbo.MyStoredProcedure

The EXEC takes the place of a SELECT statement. EXEC取代了SELECT语句。 To get the value, just SELECT from the table you inserted into. 要获取值,只需从您插入的表中选择SELECT。 Typically, this would be a temp table. 通常,这将是临时表。

The other option is to convert the stored procedure that returns a recordset into a function that returns a table. 另一种选择是将返回记录集的存储过程转换为返回表的函数。

Ant's approach is probably best if you want to minimize the changes to your system. 如果您想最小化对系统的更改,Ant的方法可能是最好的。

Normally you would use a temporary table for that approach since you can't use an exec statement to insert into a table variable. 通常,您将使用临时表来执行该方法,因为您无法使用exec语句插入表变量。

Here's a variation which will work well if you need to use this for MULTIPLE recordsets. 如果你需要将它用于MULTIPLE记录集,这是一个很好的变体。

CREATE TABLE #outsidetable (...)
exec spInsideProcedure
SELECT * FROM #outsidetable

inside spInsideProcedure 在spInsideProcedure里面

INSERT INTO #outsidetable SELECT <blah blah blah>

I tried Ant's approach and it worked a treat: 我尝试了Ant的方法,它有所作为:

Declare @Success tinyint
Declare @Response Table (Success int)
Insert into @Response(Success)
Exec Fix_RollbackReturn 12345, 15
Select @Success=Success from @Response

As you can see I used a Table Variable rather than a temporary table because slightly more efficient than a temporary table. 如您所见,我使用了表变量而不是临时表,因为它比临时表略高效。

Thanks for all your help guys. 谢谢你们的帮助。

EDIT: It appears that Dave was right after all. 编辑:看来Dave是对的。 That is, my Exec-into-Table-variable approach worked on my SQL2005 development machine, but when moved to the Live (SQL2000) machine it objected, so I had to change to the temporary table approach. 也就是说,我的Exec-into-Table-variable方法适用于我的SQL2005开发机器,但是当它转移到Live(SQL2000)机器时它反对,所以我不得不改为临时表方法。

It's a little annoying, especially since in a couple of weeks we are upgrading to SQL2005 across the board(!). 这有点烦人,特别是因为在几周内我们全面升级到SQL2005(!)。

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

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