Microsoft KB Archive/810100

= BUG: &quot;No Corresponding BEGIN TRANSACTION&quot; Error When COMMIT TRANSACTION Is Run =

Article ID: 810100

Article Last Modified on 11/13/2006

-

APPLIES TO


 * Microsoft ActiveX Data Objects 2.7
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



SYMPTOMS
When you call a recursive SQL Server stored procedure from an ADO application by using ODBC CALL syntax in a TRANSACTION, you may receive the following error message when COMMIT TRANSACTION is run:

The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION. (Microsoft OLE DB Provider for SQL Server)



CAUSE
ADO causes Microsoft SQL Server to run the stored procedure on two occasions. First, it runs the procedure with SET FMTONLY set to ON. When SET FMT ONLY is set to ON, SQL Server always exceeds the maximum nesting level (32) irrespective of the parameter that is passed. Therefore, SQL Server aborts the batch, and rolls back the TRANSACTION. Because the SQL Server internally runs this method, no error is reported. SQL Server runs the stored procedure again. This time, depending on the parameter that is passed, the execution of the stored procedure occurs.

When this second execution of the stored procedure exceeds the 32-level limit, the correct error message is returned. When the correct execution of the stored procedure does not exceed the nesting level maximum, you receive the error message that is described in the &quot;Symptoms&quot; section. This occurs when COMMIT TRANSACTION is run. It is run because SQL Server rolled back the TRANSACTION when the maximum nesting was exceeded, and SQL Server aborted the batch.



RESOLUTION
To work around this problem, do not call the stored procedure by using ODBC Call syntax. You can call the stored procedure in may ways by using ADO. One of the methods is to call a stored procedure by using a command object. The following is an example: ' Command to execute the stored procedure Dim cmd As ADODB.Command Set cmd = New ADODB.Command With cmd ' Specify the command type as Stored Procedure .CommandType = adCmdStoredProc ' Specify the Stored Procedure to call .CommandText = &quot;RecursionTest&quot; ' Specify the Connection to be used Set .ActiveConnection = cn     ' Set up new parameter for the stored procedure Dim prm As Parameter Set prm = cmd.CreateParameter(&quot;@Param&quot;, adInteger, adParamInput,, -35) .Parameters.Append prm ' Execute the Stored Procedure .Execute End With Set cmd = Nothing



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce the Behavior
 Open SQL Query Analyzer.  In the Northwind sample database, run the following SQL script to create a recursive stored procedure: CREATE PROCEDURE RecursionTest @Param Int As Set @Param = @Param + 1 If @Param < 3 Exec RecursionTest @Param  Create a new Standard EXE project in Visual Basic 6.0. By default, Form1 is created. On the Project menu, click References. In the References list, double-click Microsoft ActiveX Data Objects 2.7 Library, and then click OK. In the toolbox, double-click CommandButton to add the CommandButton to the Form. By default, Command1 is created.</li> Double-click Button1 to add code for the click event of Command1.</li>  Replace the existing code in the Command1_Click event with the following code: Private Sub Command1_Click ' Connect to SQL Server Dim cn As ADODB.Connection Set cn = New ADODB.Connection cn.Open &quot;Provider=SQLOLEDB; Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI&quot; ' Begin a Transaction cn.Execute &quot;BEGIN TRANSACTION&quot;,, adExecuteNoRecords ' Create a Command to execute Stored Procedure Dim cmd As ADODB.Command Set cmd = New ADODB.Command With cmd strSQL = &quot;{ call RecursionTest (?)}&quot; .CommandType = adCmdText .CommandText = strSQL Set .ActiveConnection = cn     .Prepared = True .Parameters(0) = -3 ' Execute the Stored Procedure .Execute End With Set cmd = Nothing ' Commit the Transaction cn.Execute &quot;COMMIT TRANSACTION&quot;,, adExecuteNoRecords cn.Close Set cn = Nothing End Sub </li> On the Run menu, click Start to run the application.</li> Click Command1.</li></ol>

<div class="references_section">