Tuesday, May 8, 2012

How do I deal with multiple resultsets from a stored procedure?

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 
    ' ... 
%>

No comments:

Post a Comment