views:

2837

answers:

3

I have a procedure that returns multiple tables; eg:

PROCEDURE Something AS
BEGIN
 SELECT 1,2,3
 SELECT 4,5
 SELECT 9,10,11
END

I would like to take each table from the result and insert it into a series of tables/temp tables - one for each record set.

Is this possible?

+1  A: 

if you Union the results together they would come out as one result set.

your second query only has 2 columns but this would need to be resolved either way as you put it into a table.

Jeff Martin
I had considered that however unions are not possible because the schema differs
Then what are you expecting to happen by putting them all in one table?
lc
I agree with @IC, if you are putting them into one table, you would have to account for the different schema in some way.
Jeff Martin
Sorry if i wasnt clear; I would like each set in its own table.
+1  A: 

You could create the temporary tables within the stored proc and push the records into that. If you are using the same session , the table would be available after the stored proc is finished.

Or you could create the temp tables before hand and call the sp to populate them.

Learning
If you create the #TempTables in the child SProc are they still in scope for the Parent? I have a feeling they have to be created in the Parent, and then used in the Child, for the results to be in scope for the Parent again
Kristen
It works both ways. Since temp tables are like global variables witin the session , they can be seen from everywhere in the session.
Learning
A: 

Check out Multiple Active Resultsets (MARS). It may do what you are looking for.

http://www.sqlteam.com/article/multiple-active-result-sets-mars

http://blogs.msdn.com/sqlprogrammability/archive/2006/05/01/MARSIntroduction1.aspx

Darrel Miller
I may be wrong, but I thought MARS allowed launching a second query before the first one had been fully retrieved - rather than just multiple resultsets from a single command execution?
Kristen
You may be absolutely right. I just figured that if there was any way to do what the OP was trying to do then chances are it would be using MARS.
Darrel Miller