PRB: ADO Not Returning @@IDENTITY Value After AddNew (195224)
The information in this article applies to:
- ActiveX Data Objects (ADO) 1.5
- ActiveX Data Objects (ADO) 2.0
- ActiveX Data Objects (ADO) 2.1 SP2
- ActiveX Data Objects (ADO) 2.5
- ActiveX Data Objects (ADO) 2.6
- ActiveX Data Objects (ADO) 2.7
This article was previously published under Q195224 SYMPTOMS ActiveX Data Objects (ADO) does not return the @@IDENTITY
value after executing a Recordset.Addnew method or SQL Insert statement.
CAUSE Here are two distinct reasons for this behavior:
- For SQL Insert statements, ODBC version 3.5 changed the
behavior with respect to SQLMoreResults such that, "output parameters aren't
stored in the application's buffer until after the app calls SQLMoreResults and
it returns SQL_NO_DATA_FOUND. The ODBC 2.65 driver would read-ahead and
sometimes lump result sets together or skip over them. The ODBC 3.5 driver was
changed to provide result sets in a consistent fashion w/o the various problems
that used to occur."
NOTE: To avoid calling SQLMoreResults on such statements, which would
suppress the @@IDENTITY, you must use the SET NOCOUNT ON in the SQL Insert
statement.
- For the Recordset.AddNew method, if the server cursor is
built with dbcursoropen for a table not containing a unique index, the server
cursor is read-only and a temporary table is created in Tempdb. Subsequent
cursor fetches will be on the temporary table. Therefore, changes made by
others to the rows in the base table will not be visible through the
cursor.
RESOLUTION If you use SET NOCOUNT ON in your SQL statement or a Unique
Index on the table for an insert with the Recordset.AddNew method, the IDENTITY
value returns as expected. STATUS This behavior is by design. REFERENCES ODBC 3.0 Programmer's Reference, volume II; topic:
"SQLMoreResults", pg. 830, Microsoft Press
Inside Microsoft SQL
Server 6.5, pg. 552 - 559.
SQL Server Books Online; topic:
"dbcursoropen", topic: "Unique Index"
Modification Type: | Major | Last Reviewed: | 8/10/2006 |
---|
Keywords: | kbDatabase kbprb KB195224 |
---|
|