question

datadanny avatar image
datadanny asked

Separate Stored Procedure result sets into Temporary tables

I have a a stored procedure that returns 2 result sets. I'm trying to call the stored procedure and insert the values of the first result set into a temporary table, but I am unable to do this because I'm returning 2 result sets. Is there a way to return just one result set or both of them into seperate temporary tables. I am unable to change the stored procedure. Stored procedure result set 1
column a | column b | coulmn c
Stored procedure result set 2
column x | column y
What I am doing is
DECLARE @ResultSet1Table 
TABLE (
        column a
       ,column b
       ,column c
       )

INSERT INTO @ResultSet1Table 
   EXEC StoredProc

And receiveing the error message 'Column name or number of supplied values does not match table definition' because of the second result set.
stored-procedurestsql
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
ASK SQL Server Central runs by you voting. For all helpful answers below, please indicate this by clicking on the thumbs up next to those answers. If any one answer lead to a solution, please indicate this by clicking on the check mark next that answer.
0 Likes 0 ·
Kev Riley avatar image
Kev Riley answered
As it stands no. Using `INSERT INTO ... EXEC` the result sets must be compatible with the columns in the table. One way around this maybe is to define the table columns with a datatype that will (taking the first column as an example) accept both column a and column x. This would put both result sets into the table, and then maybe you can query over that to return the data that is just the first result set e.g. `[column c] is not null` Also you cannot use a table variable as the target of an `INSERT ... EXEC`, it must be a #temp table or a permanent table. [***CORRECTION* I should have said a table-valued parameter, not a table variable**]
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ThomasRushton avatar image ThomasRushton ♦♦ commented ·
Looks as though you can't pass Table-value parameters to be populated by the SP, either, as those can't be modified. Shame.
0 Likes 0 ·
srutzky avatar image
srutzky answered
> Is there a way to return just one result set Yes, but not in pure T-SQL. You can create a SQLCLR stored procedure or function that creates a `SqlConnection` and executes `SqlCommand.ExecuteReader()`. The behavior of ExecuteReader is to cycle through one result set at a time. So if you never call `SqlDataReader.NextResult()`, then you will have only retrieved the first result set. Along these same lines, if you only want result set #2, you can start by issuing `SqlDataReader.NextResult()` to skip the first result set and then iterate through `SqlDataReader.Read()`. **EDIT:** Please keep in mind if choosing to do this via a SQLCLR Stored Procedure: taking the short-cut of passing the `SqlDataReader` to `SqlContext.Pipe.Send()` in order to avoid having to write out the full iteration over each row and calling `SqlContext.Pipe.SendResultsRow()` is _not_ an option. The reason is that, even though you can skip any number of result sets by calling `SqlDataReader.NextResult()` prior to calling `SqlContext.Pipe.Send()`, it will automatically call `SqlDataReader.NextResult()` and hence return any remaining result sets. This is fine if you want to only return the last 1 (or more) result sets out of two or more, but does not help when wanting to limit the number of result sets that will be returned (which is what this question is asking to do). --- Also, regarding @Kev's statement of: > Also you cannot use a table variable as the target of an INSERT ... EXEC, it must be a #temp table or a permanent table. That is not true as of SQL Server 2005. You can definitely do an INSERT...EXEC into a table variable: CREATE PROCEDURE #Proc AS SET NOCOUNT ON; SELECT 1 AS [b]; GO DECLARE @Table TABLE (Col1 INT); INSERT INTO @Table (Col1) EXEC #Proc; SELECT * FROM @Table;
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
So you can - I was mis-reading BOL. I did think it was a strange limitation, and I should have tested. Thanks!
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.