Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 294160 - Last Review: November 30, 2007 - Revision: 4.3
BUG: Update of Newly Inserted Row Causes DB_E_ERRORSOCCURRED
This article was previously published under Q294160
On updating a row which has been newly inserted into a table, whether through an OLEDB rowset or an ActiveX Data Objects (ADO) recordset, the following error may occur:
(0x80040e21, DB_E_ERRORSOCCURRED) Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.
This error occurs when the following conditions are met:
- There is a connection to SQL Server through the OLE DB Provider for ODBC drivers (MSDASQL).
- The recordset is created by selecting from a SQL Server database table.
- The recordset uses server-side cursors.
- One of the columns in the recordset is the primary key for the table.
- There is an insert trigger on the table which inserts rows into some database object.
- The AddNew and Update methods are used to add a new row to the table.
- Update is called again to update a row before the recordset has been scrolled using FetchNext, FetchFirst, and so on.
To prevent this error from occurring, do any of the following:
- Use client-side cursors (by setting the ADO recordset Cursor Location property to adUseClient).
- Remove the insert trigger from the source table.
- Put the following line at the beginning of the trigger code.
- Use SQLOLEDB rather than MSDASQL, and set the "Change Inserted Rows" property of the Recordset object to true before opening the recordset:
pRs.Properties.Item("Change Inserted Rows") = True
- Remove the primary key from the table.
- After calling Update the first time to insert the new row in the table, scroll the recordset using methods like MoveNext and MovePrevious before calling Update again.
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.
Steps to Reproduce Behavior
- Use the following script to create the database objects on SQL Server 7.0 or SQL Server 2000:
create table ADOtest(
col1 int not null primary key
)
go
create table ADOtest2(
col1 int not null primary key
)
go
create trigger trIADOtest on ADOtest for insert as
insert ADOtest2 values (7)
go
- Use the following code in a Microsoft Visual Basic application to reproduce the error. Be sure to change the DSN name, user name, and password.
Dim pConn As New ADODB.Connection
pConn.Open "dsn=YOURDSN", "USERNAME", "PASSWORD", adConnectUnspecified
'The following two lines are just to clean up from previous runs.
pConn.Execute "DELETE FROM ADOTEST"
pConn.Execute "DELETE FROM ADOTEST2"
Dim pRs As New ADODB.Recordset
pRs.ActiveConnection = pConn
pRs.Open "SELECT * FROM ADOtest", , adOpenKeyset, adLockOptimistic, adCmdUnspecified
pRs.AddNew
pRs!col1 = 3
pRs.Update
pRs!col1 = 4
pRs.Update '<---- Error occurs here.
APPLIES TO
- Microsoft SQL Server 2000 Standard Edition
- Microsoft Data Access Components 2.5
- Microsoft Data Access Components 2.5 Service Pack 1
- Microsoft Data Access Components 2.6
- Microsoft SQL Server 7.0 Standard Edition
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