Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
I have a stored proc named A
which is returning result as 
		select '1' as 'id', 'success' as description
		
I am calling this Proc from another proc named B
		I want to collect this result of A
		
		How do i do it?


What I have tried:

I did lot of google search but did not find the relevant answer
Posted
Updated 29-Nov-16 0:08am
Comments
Afzaal Ahmad Zeeshan 24-Nov-16 7:18am    
So, did it work?
Vivek S Kale 24-Nov-16 7:43am    
no

SQL
DECLARE @ParmDefinition nvarchar(500)=N'@retvalOUT VARCHAR(500) OUTPUT',@retval VARCHAR(500) 

SQL
DECLARE @SqlTxt NVARCHAR(MAX)='Put your SQL Query here';

EXECUTE sp_executesql @SqlTxt, @ParmDefinition, @retvalOUT=@retval OUTPUT;
Select @retval;  --It worked for me to get output


You can modify it according to your's requirement
 
Share this answer
 
SQL
CREATE PROCEDURE [dbo].[ProcA] 	
AS
BEGIN	
	SET NOCOUNT ON;    
	select 1 as 'id', 'success' as description
END

SQL
CREATE TABLE [dbo].[SampleData](
	[id] [int] NULL,
	[description] [varchar](50) NULL
)

SQL
CREATE PROCEDURE [dbo].[ProcB] 	
AS
BEGIN	
	SET NOCOUNT ON;
    Insert into SampleData exec ProcA
END

For more details refer Is it possible to pass TABLE as the output parameter in stored procedure[^]
 
Share this answer
 
Create SP and add below script

SQL
INSERT INTO #tab EXEC Procedure


and Select * from #tab
 
Share this answer
 
v2
Hi Vivek, form my view inserting data directly from stored procedure output into temp tables is a bad practice. This is because, in this case, you should be aware that every time you update the output of your stored procedure (by adding or deleting new columns for example) you must update the structure of temp tables in all other procedures that are calling it. If not, an error will be thrown owing to different columns in temp table and stored procedure output.

So, I would recommend you, if possible, to convert the stored procedure into a table-valued function, and finally inserting data in other procedures making use of this function.

Finally, you can call the function by writing 'select * into #tempTable from function(params)' or by creating a table variable previously and then inserting data into it. In this last case, it's a good practice to specify all the columns in the code, both in 'insert' statement and 'select' statement, trying to avoid massive use of "*" operator.


Regards.
 
Share this answer
 
SQL
CREATE TABLE #TEMP( -- Create a temp table with same definition
	[id] [int],
	[description] [varchar](MAX) 
)
Insert into #TEMP   -- Inserting the data returned from Proc A
exec ProcA <parameters...>

SELECT * FROM #TEMP -- Read the data like this wherever you need. 
DROP TABLE #TEMP   -- you can drop at last


If you really don't want to create a temp table, because of its dynamic column definition. then should try below link which is not advisable .
Click here
 
Share this answer
 
v2
you have to return the id and description?
beacause the best way is to return only the column ID.
SQL
DECLARE	@id int

EXEC	@id = [dbo].[sp_your_proc]

Obviously you have to use return clause in sp_your_proc.
let me know if it helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900