Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to Result sets from within Stored procedures Transact-SQL SQL Server

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?

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 

I need to be able to iterate through both result sets individually.

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.

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

like image 787
Matthew Farwell Avatar asked Sep 12 '08 13:09

Matthew Farwell


People also ask

How do I select the results of a stored procedure?

The only way to work with the results of a stored procedure in T-SQL is to use the INSERT INTO ... EXEC syntax. That gives you the option of inserting into a temp table or a table variable and from there selecting the data you need.

How can I return multiple values from a stored procedure in SQL Server?

In order to fetch the multiple returned values from the Stored Procedure, you need to make use of a variable with data type and size same as the Output parameter and pass it as Output parameter using OUTPUT keyword. You can also make use of the Split function to split the comma separated (delimited) values into rows.

How can we get multiple result sets from a stored procedure using Entity Framework?

In order to get multiple result sets working we need to drop to the ObjectContext API by using the IObjectContextAdapter interface. Once we have an ObjectContext then we can use the Translate method to translate the results of our stored procedure into entities that can be tracked and used in EF as normal.


2 Answers

The short answer is: you can't do it.

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.

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:

INSERT INTO #Table (...columns...) EXEC MySproc ...parameters... 

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.

like image 84
Brannon Avatar answered Oct 06 '22 07:10

Brannon


I was easily able to do this by creating a SQL2005 CLR stored procedure which contained an internal dataset.

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. dataset.ReadXmlSchema will show you the schema of each result set.

Step 1: Begin writing the sproc which will read the data from the multi-result-set sproc

a. Create a separate table for each result set according to the schema.

CREATE PROCEDURE [dbo].[usp_SF_Read] AS SET NOCOUNT ON; CREATE TABLE #Table01 (Document_ID VARCHAR(100)   , Document_status_definition_uid INT   , Document_status_Code VARCHAR(100)    , Attachment_count INT   , PRIMARY KEY (Document_ID)); 

b. At this point you may need to declare a cursor to repetitively call the CLR sproc you will create here:

Step 2: Make the CLR Sproc

Partial Public Class StoredProcedures     <Microsoft.SqlServer.Server.SqlProcedure()> _     Public Shared Sub usp_SF_ReadSFIntoTables()      End Sub End Class 

a. Connect using New SqlConnection("context connection=true").

b. Set up a command object (cmd) to contain the multiple-result-set sproc.

c. Get all the data using the following:

    Dim dataset As DataSet = New DataSet     With New SqlDataAdapter(cmd)         .Fill(dataset) ' get all the data.     End With 'you can use dataset.ReadXmlSchema at this point... 

d. Iterate over each table and insert every row into the appropriate temp table (which you created in step one above).

Final note: In my experience, you may wish to enforce some relationships between your tables so you know which batch each record came from.

That's all there was to it!

~ Shaun, Near Seattle

like image 28
Shuggmeister Avatar answered Oct 06 '22 07:10

Shuggmeister