Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSQL: Call a stored procedure from another stored procedure and read the result

I have a stored procedure that, ending with a SELECT, returns a recordset. I can call it within anoher stored procedure like this:

EXEC procedure @param 

How to get the returning recordset? Thanks

like image 438
pistacchio Avatar asked Jun 14 '10 16:06

pistacchio


People also ask

How do you call a procedure from another procedure?

If you are trying to call the procedure get_manager_details inside test_procedure then you first need to create the test procedure. Add create or replace procedure test_procedure . Then after creating the test_procedure you can execute it in an anonymous block which will call the get_manager_details procedure.


2 Answers

You can create a temp table and then use INSERT INTO #MyTable EXEC procedure @param.

There are some other techniques listed here.

like image 141
Ben Hoffstein Avatar answered Oct 10 '22 00:10

Ben Hoffstein


AFAIK, you can't. What you probably want to do is use a function for your first (or both) procedures. Functions can only return one thing, but they can return a table. Stored procedures can return multiple results, but not to other functions/stored procedures.

e.g.:

CREATE FUNCTION [dbo].[fn_GetSubordinates] (     @sPersonID VARCHAR(10),     @nLevels INT ) RETURNS @tblSubordinates TABLE (     Person_Id VARCHAR(10),     Surname char(25),     Firstname char(25) ) AS BEGIN     ... 
like image 26
Kendrick Avatar answered Oct 09 '22 22:10

Kendrick