Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

can a sql store procedure return more than one table .

how?



Thanks
Posted

yes, it can

create procedure sp_return2tables
As
Select * from table1
Select * from table2
GO


It will do, on the front-end you can fetch the values from dataset1 and dataset2.
I hope this will solve your requirement.

Anurag
 
Share this answer
 
Comments
manika123 21-Jul-11 6:20am    
I agreed.
but if i have to return 1 lacs table then ?(actually one interviewer asked me that ques.)
@nuraGGupta@ 21-Jul-11 7:38am    
This command can return you the list of all the tables in a database:
SELECT * FROM sysobjects WHERE xtype='U'
use a while loop or a cursor to iterate through each table name and return the values from them.
@nuraGGupta@ 21-Jul-11 8:53am    
You can click on "Mark as Solution" button if you found the answer useful. [;)]
Uday P.Singh 21-Jul-11 12:46pm    
my 5!
@nuraGGupta@ 21-Jul-11 14:38pm    
Thanks Uday
Yes.
CREATE PROCEDURE spSomeStoredProcedure AS 
BEGIN

Select * from table1

Select * from table2

Select * from table3

END
GO 


and check this also.

http://shan-tech.blogspot.com/2005/08/sql-server-stored-procedures-returning.html[^]
 
Share this answer
 
v2
yes the above ans are correct ... but technically speaking they don't return the tables ..... as an output ..... since because they don't have there return type as a Table ..... but they process the query and give the table as an output .... which if you want to capture .... you an use the ADO .NET and retrieve all the tables processed by it by the code that they have given ......
 
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