Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 253240 - Last Review: December 3, 2003 - Revision: 4.1
PRB: Errors Not Returned When Stored Procedure Statement Fails
This article was previously published under Q253240
When executing a stored procedure with multiple queries (SELECTs, INSERTs and UPDATEs), the ActiveX Data Objects (ADO) errors collection appears not to be populated after an error occurs in the stored procedure. The stored procedure might also not return values from its RETURN statement.
The SQL Server OLE DB provider provides more accurate information (than ODBC) to the caller about what happened in the procedure. Each SQL statement within a stored procedure returns a "result," either a count of rows affected or a result set. You can walk through these result sets in ADO using the NextRecordset method on the Recordset object.
The SQL Server ODBC provider, however, does not provide information about the results of individual SQL statements within a stored procedure. The only result that comes back from a stored procedure execution is the result of the SELECT statement, if it has one. This is why this problem may not manifest with ODBC.
In both cases, return values from the stored procedure may not be returned.
To resolve this problem, make sure that the SET NOCOUNT ON statement comes before any other SQL statements in the stored procedure, as in the following:
CREATE PROCEDURE TestProc
AS
SET NOCOUNT ON -- This is off by default
SELECT au_lname FROM Authors
RETURN 0
This behavior is by design.
Steps to Reproduce the Behavior
- Create an ASP page with the following code:
Note You must change UserID=<username> to the correct value before you run this code. Make sure that the User ID has the appropriate permissions to perform this operation on the database.
<%
Set oConn = Server.CreateObject("ADODB.Connection")
Set Rs = Server.CreateObject("ADODB.Recordset")
On Error Resume Next
oConn.Open("Provider=SQLOLEDB.1;User ID=<username>;Initial Catalog=Northwind;Data Source=DataSourceName")
Set oCmd = Server.CreateObject("ADODB.Command")
Set oCmd.ActiveConnection = oConn
oCmd.CommandText = "TestProc"
oCmd.CommandType = adCmdStoredProc
oCmd.Parameters.Append oCmd.CreateParameter("RETURN_VALUE",3,4,0)
oCmd.Execute()
If oCmd.ActiveConnection.Errors.Count > 0 Then
For Each oError in oCmd.ActiveConnection.Errors
Response.Write "Description = " & oError.Description & "<BR>"
Next
End If
Response.Write( "Return Value = " & oCmd.Parameters("RETURN_VALUE") & "<BR>")
oConn.Close
Set oConn = Nothing
Set oCmd = Nothing
%>
- Create a stored procedure similar to the following:
ALTER Procedure TestProc AS
-- Uncommenting the line below resolved this problem
-- set nocount ON
begin tran
insert into xxx values(1)
If @@error <> 0
Begin
rollback
return 1
End
insert into yyy values(<Invalid Data>)
if @@error <> 0
begin
rollback
return 2
end
insert into zzz values(3)
if @@error <> 0
begin
rollback
return 3
end
commit tran
return 0
- Run the ASP page, and you should get the following result instead of an error message and a return value of 2.
Return Value =
APPLIES TO
- Microsoft Active Server Pages 4.0
- Microsoft Data Access Components 2.0
- Microsoft Data Access Components 2.1
- Microsoft Data Access Components 2.1 Service Pack 2
- Microsoft Data Access Components 2.1 Service Pack 1
- Microsoft Data Access Components 2.1 Service Pack 2
- Microsoft Data Access Components 2.5
- Microsoft Data Access Components 2.6
- Microsoft SQL Server 6.5 Standard Edition
- Microsoft SQL Server 7.0 Standard Edition
| kbcodesnippet kbdatabase kberrmsg kbprb KB253240 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate