Symptoms
SQL Server stored procedures are capable of returning more than one recordset and the Remote Data Object (RDO) has the ability to access these Multiple Resultsets.
When calling these stored procedures, the following error can be encountered:
Error 40002
“37000: [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open a cursor on a stored procedure that has anything other than a single
select statement in it” The following code sample showing how to return the multiple Resultsets using the MoreResults Property of the RDO.
Resolution
There are two methods to avoid Error 40002.
Method 1 Use The ODBC Cursor library rather than Server Side Cursors. To do this, use the following code:
rdoEngine. rdoDefaultCursorDriver = rdUseODBC
- or -
rdoEnvironments(0).CursorDriver = rdUseOdbc This option gives better performance for small result sets, but may degrade quickly for larger result sets depending on the Server and workstation configuration.
NOTE: If you are using SQL text fields, you must use Server-side cursors to bypass Error 40002″
Method 2 Use Server Side Cursors, a Forward Only Cursor, and a rowset size of 1. Make the server create a cursor-less resultset on the server side by using a forward only cursor and a RowSetSize of 1.
The following code sample illustrates how to create a stored procedure that returns multiple result sets using method 2. Create the stored procedure on SQL Server:
Start the ISQL utility that shipped with SQL server. Connect to your SQL server. Select the pubs database from the combo box labeled DB. Enter the following lines into the Query Tab:
CREATE PROCEDURE TestMultiResults ASselect * from authorsselect * from discountsGO Choose the Query|Execute menu. The Results tab should display:
This command did not return data, and it did not return any rows
A stored procedure called TestMultiResults has now been created in the pubs database on SQL Server. Create the Visual Basic client to call the stored procedure:
Start Visual Basic. Form1 is created by default. Add a Command Button (Command1) and a List Box (List1) to Form1. Add the following code to Form1:
Private Sub Form_Load()Command1.Caption = “Run Stored Procedure”End SubPrivate Sub Command1_Click()Dim cn As rdoConnectionDim ps As rdoPreparedStatementDim rs As rdoResultsetDim strConnect As String’set cursor driver to use server-side cursorsrdoDefaultCursorDriver = rdUseServer’open a connection to the pubs database using DSNless connections’Remember to change the following connection string parameters to reflect the correct valuesstrConnect = “Driver={SQL Server}; Server=myServer; ” & _”Database=pubs; Uid=<username>; Pwd=<strong password>”Set cn = rdoEnvironments(0).OpenConnection(dsName:=”", _Prompt:=rdDriverNoPrompt, _ReadOnly:=False, _Connect:=strConnect)’create a prep stmt for the stored proc callSet ps = cn.CreatePreparedStatement(“MyPs”, _”{call TestMultiResults}”)’set the RowSet size to 1ps.RowsetSize = 1′open the resultset with forward-only cursorSet rs = ps.OpenResultset(rdOpenForwardOnly)’add the first resultset to a list boxWhile Not rs.EOFlist1.AddItem rs(“au_fname”) & ” ” & rs(“au_lname”)rs.MoveNextWend’move to the second resultsetrs.MoreResultslist1.AddItem “Second Resultset Below”‘add the second resultset to the same list boxWhile Not rs.EOFlist1.AddItem rs(“discounttype”) & ” = ” & rs(“discount”)rs.MoveNextWend’Close the resultset and the connection and set both to nothingrs.CloseSet rs = Nothingcn.CloseSet cn = NothingEnd Sub Run the project and click the “Run Stored Procedure” button. You should see a list of Authors and then Discounts in the list box.