<p>I'm using SQL Server 2005, and I would like to know how to access different result sets from within transact-sql. The following stored procedure returns two result sets, how do I access them from, for example, another stored procedure?</p>

<pre><code>CREATE PROCEDURE getOrder (@orderId as numeric) AS
BEGIN   
    select order_address, order_number from order_table where order_id = @orderId
    select item, number_of_items, cost from order_line where order_id = @orderId
END
</code></pre>

<p>I need to be able to iterate through both result sets individually.</p>

<p>EDIT: Just to clarify the question, I want to test the stored procedures. I have a set of stored procedures which are used from a VB.NET client, which return multiple result sets. These are not going to be changed to a table valued function, I can't in fact change the procedures at all. Changing the procedure is not an option.</p>

<p>The result sets returned by the procedures are not the same data types or number of columns.</p>

[{'answer_id': 58960, 'author': 'Kilhoffer', 'author_id': 5469, 'author_profile': 'https://Stackoverflow.com/users/5469', 'pm_score': 0, 'selected': False, 'text': '<p>You could select them into temp tables or write table valued functions to return result sets. Are asking how to iterate through the result sets?</p>\n'}, {'answer_id': 58973, 'author': 'Josef', 'author_id': 5581, 'author_profile': 'https://Stackoverflow.com/users/5581', 'pm_score': 1, 'selected': False, 'text': "<p>There are two ways to do this easily. Either stick the results in a temp table and then reference the temp table from your sproc. The other alternative is to put the results into an XML variable that is used as an OUTPUT variable.</p>\n\n<p>There are, however, pros and cons to both of these options. With a temporary table, you'll need to add code to the script that creates the calling procedure to create the temporary table before modifying the procedure. Also, you should clean up the temp table at the end of the procedure.</p>\n\n<p>With the XML, it can be memory intensive and slow.</p>\n"}, {'answer_id': 59015, 'author': 'Brannon', 'author_id': 5745, 'author_profile': 'https://Stackoverflow.com/users/5745', 'pm_score': 6, 'selected': True, 'text': "<p>The short answer is: you can't do it.</p>\n\n<p>From T-SQL there is no way to access multiple results of a nested stored procedure call, without changing the stored procedure as others have suggested.</p>\n\n<p>To be complete, if the procedure were returning a single result, you could insert it into a temp table or table variable with the following syntax:</p>\n\n<pre><code>INSERT INTO #Table (...columns...)\nEXEC MySproc ...parameters...\n</code></pre>\n\n<p>You can use the same syntax for a procedure that returns multiple results, but it will only process the first result, the rest will be discarded.</p>\n"}, {'answer_id': 67369, 'author': 'Chris Wuestefeld', 'author_id': 10082, 'author_profile': 'https://Stackoverflow.com/users/10082', 'pm_score': 2, 'selected': False, 'text': '<p>Note that there\'s an extra, undocumented limitation to the INSERT INTO ... EXEC statement: it cannot be nested. That is, the stored proc that the EXEC calls (or any that it calls in turn) cannot itself do an INSERT INTO ... EXEC. It appears that there\'s a single scratchpad per process that accumulates the result, and if they\'re nested you\'ll get an error when the caller opens this up, and then the callee tries to open it again.</p>\n\n<p>Matthieu, you\'d need to maintain separate temp tables for each "type" of result. Also, if you\'re executing the same one multiple times, you might need to add an extra column to that result to indicate which call it resulted from.</p>\n'}, {'answer_id': 269976, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>Sadly it is impossible to do this. The problem is, of course, that there is no SQL Syntax to allow it. It happens 'beneath the hood' of course, but you can't get at these other results in TSQL, only from the application via ODBC or whatever. </p>\n\n<p>There is a way round it, as with most things. The trick is to use ole automation in TSQL to create an ADODB object which opens each resultset in turn and write the results to the tables you nominate (or do whatever you want with the resultsets). you can also do it in DMO if you enjoy pain. </p>\n"}, {'answer_id': 879516, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>I was easily able to do this by creating a SQL2005 CLR stored procedure which contained an internal dataset.</p>\n\n<p>You see, a new SqlDataAdapter will .Fill a multiple-result-set sproc into a multiple-table dataset by default.  The data in these tables can in turn be inserted into #Temp tables in the calling sproc you wish to write.  <strong>dataset.ReadXmlSchema</strong> will show you the schema of each result set.</p>\n\n<p><strong>Step 1: Begin writing the sproc which will read the data from the multi-result-set sproc</strong></p>\n\n<p>a. Create a separate table for each result set according to the schema.</p>\n\n<pre><code>CREATE PROCEDURE [dbo].[usp_SF_Read] AS\nSET NOCOUNT ON;\nCREATE TABLE #Table01 (Document_ID VARCHAR(100)\n  , Document_status_definition_uid INT\n  , Document_status_Code VARCHAR(100) \n  , Attachment_count INT\n  , PRIMARY KEY (Document_ID));\n</code></pre>\n\n<p>b. At this point you may need to declare a cursor to repetitively call the CLR sproc you will create here:</p>\n\n<p><strong>Step 2: Make the CLR Sproc</strong></p>\n\n<pre><code>Partial Public Class StoredProcedures\n    &lt;Microsoft.SqlServer.Server.SqlProcedure()&gt; _\n    Public Shared Sub usp_SF_ReadSFIntoTables()\n\n    End Sub\nEnd Class\n</code></pre>\n\n<p>a. Connect using <code>New SqlConnection("context connection=true")</code>.</p>\n\n<p>b. Set up a command object (cmd) to contain the multiple-result-set sproc. </p>\n\n<p>c. Get all the data using the following:</p>\n\n<pre><code>    Dim dataset As DataSet = New DataSet\n    With New SqlDataAdapter(cmd)\n        .Fill(dataset) \' get all the data.\n    End With\n\'you can use dataset.ReadXmlSchema at this point...\n</code></pre>\n\n<p>d. Iterate over each table and insert every row into the appropriate temp table (which you created in step one above).</p>\n\n<p><strong>Final note:</strong>\nIn my experience, you may wish to enforce some relationships between your tables so you know which batch each record came from.</p>\n\n<p>That\'s all there was to it!</p>\n\n<p>~ Shaun, Near Seattle</p>\n'}, {'answer_id': 7168255, 'author': 'Daniel Barbalace', 'author_id': 908590, 'author_profile': 'https://Stackoverflow.com/users/908590', 'pm_score': 3, 'selected': False, 'text': "<p>There is a kludge that you can do as well.  Add an optional parameter N int to your sproc.  Default the value of N to -1.  If the value of N is -1, then do every one of your selects.  Otherwise, do the Nth select and only the Nth select.</p>\n\n<p>For example,</p>\n\n<pre><code>if (N = -1 or N = 0)\n    select ...\n\nif (N = -1 or N = 1)\n    select ...\n</code></pre>\n\n<p>The callers of your sproc who do not specify N will get a result set with more than one tables.  If you need to extract one or more of these tables from another sproc, simply call your sproc specifying a value for N.  You'll have to call the sproc one time for each table you wish to extract.  Inefficient if you need more than one table from the result set, but it does work in pure TSQL.</p>\n"}]
['https://Stackoverflow.com/questions/58940', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1836/']