GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  Database  >  SQL Server
Go To First  |  Previous Question  |  Next Question 
 SQL Server  |  Question 51 of 94    Print  
Can we have multiple record set from stored procedure?

  
Total Answers and Comments: 3 Last Update: December 06, 2006     Asked by: Nitesh 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: reshmi
 
Yes.. stored procedure can return multiple recordsets.

Above answer was rated as good by the following members:
neel_desai
September 25, 2006 00:52:49   #1  
opbang Member Since: March 2006   Contribution: 46    

RE: can we have multiple recordset from stored procedu...

Multiple Recordsets
To create a multiple Recordset, go through the same steps as creating a typical Recordset, except, when specifying your SQL SELECT statement, put all of the SQL SELECT statements that you need in that one Recordset, separating each SQL SELECT statement with a semicolon. (You can also generate a multiple Recordset by populating a Recordset with the results of a stored procedure that returns multiple SQL queries.)

The first step to using multiple Recordsets is to making our SQL SELECT statements all appear as one, semicolon delimited string:

 

sSQL = "SELECT categoryId, categoryName FROM Categories WHERE categoryId > 3"sSQL = sSQL & ";SELECT RegionId , RegionDescription FROM Region"sSQL = sSQL & ";SELECT ShipperID, CompanyName, Phone FROM Shippers"

Now create our multiple Recordset:

sConnectString = "DRIVER={sql server};SERVER=localhost;" & _                 "DATABASE=northwind;UID=sa;PWD=" set oRS = Server.CreateObject("ADODB.Recordset")oRs.Open sSQL, sConnectString

If you like to use SQL stored procedures, you can utilize multiple Recordsets by creating a stored procedure that returns multiple SQL queries, like so:

Create Procedure MyResultsASSELECT categoryId, categoryName FROM Categories WHERE categoryId > 3SELECT RegionId, RegionDescription FROM RegionSELECT ShipperID, CompanyName, Phone FROM Shippers

And then create the multiple Recordset as follows:

sConnectString = "DRIVER={sql server};SERVER=localhost;" & _                 "DATABASE=northwind;UID=sa;PWD=" set oRS = Server.CreateObject("ADODB.Recordset")oRs.Open "MyResults", sConnectString

At this point, the oRs variable is an ADO multiple Recordset


 
Is this answer useful? Yes | No
November 08, 2006 06:49:03   #2  
reshmi        

RE: can we have multiple recordset from stored procedu...
Yes.. stored procedure can return multiple recordsets.
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
December 06, 2006 20:58:19   #3  
Ranjit Member Since: October 2005   Contribution: 18    

RE: Can we have multiple record set from stored proced...
If you have a stored procedure that looks like this: 
 
CREATE PROCEDURE myProc 
AS 
BEGIN 
    SET NOCOUNT ON 
 
    SELECT columns FROM table1 
 
    SELECT columns FROM table2 
 
    SELECT columns FROM table3 
END
 
Here is code that demonstrates how to retrieve the second and third set of results, using the NextRecordSet() method: 
 
<% 
    ' ... 
    ' assuming valid and open object, conn 
 
    set rs = conn.execute("EXEC myProc") 
 
    ' process first resultset 
 
    if not rs.eof then 
        do while not rs.eof 
            response.write rs(0) 
            rs.movenext 
        loop 
    end if 
 
    ' move to second resultset, using nextRecordSet() 
 
    set rs = rs.nextRecordSet() 
    if not rs.eof then 
        do while not rs.eof 
            response.write rs(0) 
            rs.movenext 
        loop 
    end if 
 
    ' move to third resultset, using nextRecordSet() 
 
    set rs = rs.nextRecordSet() 
    if not rs.eof then 
        do while not rs.eof 
            response.write rs(0) 
            rs.movenext 
        loop 
    end if 
    rs.close: set rs = nothing 
    ' ... 
%>

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape