Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save result of stored procedure in a Table variable [duplicate]

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

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.

What are other options to save the data that is retrieved from the call of a Stored Procedure into a variable ?

like image 599
Jan Avatar asked Jan 04 '11 15:01

Jan


People also ask

How do I return a stored procedure to a variable in SQL Server?

You can use the return statement inside a stored procedure to return an integer status code (and only of integer type). By convention a return value of zero is used for success. If no return is explicitly set, then the stored procedure returns zero. You should use the return value for status codes only.

Can stored procedure return multiple result sets?

Most stored procedures return multiple result sets. Such a stored procedure usually includes one or more select statements. The consumer needs to consider this inclusion to handle all the result sets.

How do I save changes to a stored procedure?

To save the modifications to the procedure definition, on the Query menu, select Execute. To save the updated procedure definition as a Transact-SQL script, on the File menu, select Save As. Accept the file name or replace it with a new name, and then select Save.

How do I clone a stored procedure?

Launch MS SQL Server Management Studio in your system and go to the Object Explorer. Step 2. Right-click on the database from which you want to move data and then click on Tasks>>Generate Scripts… A Generate and Publish Scripts Wizard will appear on the screen, click on the Next button to proceed.


1 Answers

Table variables are not visible to the calling procedure in the case of nested procs. The following is legal with #temp tables.

http://databases.aspfaq.com/database/should-i-use-a-temp-table-or-a-table-variable.html

http://support.microsoft.com/kb/305977/en-us

like image 66
Jahan Zinedine Avatar answered Nov 15 '22 13:11

Jahan Zinedine