Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

The problem is that the inner SP returns a record set with a single value that indicates the degree of success.

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.

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.

How (if at all) can I call this SP and determine the value of the singleton recordset returned?

Thanks

like image 565
CJM Avatar asked Jan 21 '26 21:01

CJM


1 Answers

A stored procedure returns a record set like any other, so you can actually do this:

INSERT INTO MyTable ( MyValue )

EXEC dbo.MyStoredProcedure

The EXEC takes the place of a SELECT statement. To get the value, just SELECT from the table you inserted into. Typically, this would be a temp table.

like image 161
Ant Avatar answered Jan 25 '26 06:01

Ant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!