Getting the identity value of a newly inserted row 
Author Message
 Getting the identity value of a newly inserted row

Hi,

I have a question regarding identity columns in SQL Server 7.

Sometimes, after inserting a row into a table using ADO, you would want to
get the identity created for the newly inserted row. For instance, if table
EMPLOYEE has an identity column called EmployeeId, after calling
Recordset.Update, you can read Recordset("EmployeeId").Value and get the
identity value assigned to that newly inserted row. This works okay unless
EMPLOYEE table has an insert-trigger that inserts rows to some other table.
Say, after inserting a new EMPLOYEE row, you must add into the AUDIT table
that "employee so and so has been added." , and the primary key for the
AUDIT table is another identity column (called AuditID). If EMPLOYEE table
has an insert-trigger as described above, after calling Recordset.Update
method, what you read from Recordset("EmployeeID").Value is the number that
was auto-generated for the identity column of AUDIT table, and not the
number that was auto-generated for the identity column of EMPLOYEE table. I

was added, the identity of the AUDIT table was the one that was returned
instead.

So, me and my comworker think that an alternative solution to reading the
identity value after a recordset update is by doing something like "Select
Max(EmployeeID) From EMPLOYEE". This strategy looks like it's going to work
90% of the time. But our concern is, if the database is busy (many
concurrent users doing concurrent inserts on the same tables), the "Select
Max(IdentityColumn)" strategy might not work.

I guess my main question is: Is there an efficient, bullet-proof way of
retrieving the newly created identity column value for a row that was just
inserted into a table that fires a trigger which in turn inserts a row into
another table that also has an identity column?

Any tips, recommendations would be greatly appreciated. Thanks!

-Gabe



Sun, 04 Apr 2004 04:00:14 GMT  
 Getting the identity value of a newly inserted row
I am not sure will this help
In your trigger,add following statement right after
inserting a row into Employee table


HC

Quote:
>-----Original Message-----
>Hi,

>I have a question regarding identity columns in SQL
Server 7.

>Sometimes, after inserting a row into a table using ADO,
you would want to
>get the identity created for the newly inserted row. For
instance, if table
>EMPLOYEE has an identity column called EmployeeId, after
calling
>Recordset.Update, you can read Recordset

("EmployeeId").Value and get the
Quote:
>identity value assigned to that newly inserted row. This
works okay unless
>EMPLOYEE table has an insert-trigger that inserts rows to
some other table.
>Say, after inserting a new EMPLOYEE row, you must add

into the AUDIT table
Quote:
>that "employee so and so has been added." , and the
primary key for the
>AUDIT table is another identity column (called AuditID).
If EMPLOYEE table
>has an insert-trigger as described above, after calling
Recordset.Update
>method, what you read from Recordset("EmployeeID").Value
is the number that
>was auto-generated for the identity column of AUDIT
table, and not the
>number that was auto-generated for the identity column of
EMPLOYEE table. I
>guess this behavior has something to do with ADO

referencing the global
Quote:

after EMPLOYEE row
>was added, the identity of the AUDIT table was the one
that was returned
>instead.

>So, me and my comworker think that an alternative

solution to reading the
Quote:
>identity value after a recordset update is by doing

something like "Select
Quote:
>Max(EmployeeID) From EMPLOYEE". This strategy looks like
it's going to work
>90% of the time. But our concern is, if the database is
busy (many
>concurrent users doing concurrent inserts on the same

tables), the "Select
Quote:
>Max(IdentityColumn)" strategy might not work.

>I guess my main question is: Is there an efficient,
bullet-proof way of
>retrieving the newly created identity column value for a
row that was just
>inserted into a table that fires a trigger which in turn
inserts a row into
>another table that also has an identity column?

>Any tips, recommendations would be greatly appreciated.
Thanks!

>-Gabe

>.



Mon, 05 Apr 2004 01:46:31 GMT  
 Getting the identity value of a newly inserted row
Hi Gabe,

To get the correct identity value with an insert trigger, please specify
the dynamic property: Server Data on Insert provided by Microsoft OLE DB
Provider for SQL Server (SQLOLEDB). The property specifies whether an
application can retrieve values from the database for newly inserted rows.
The following sample code may help you to solve the problem:

Note: Please use server cursor instead of client cursor and the property
only works when cursor type is adOpenKeyset.
   Dim cn As New ADODB.Connection
   Dim rs As New ADODB.Recordset
   cn.ConnectionString =
"Provider=SQLOLEDB;Server=yourServer;Database=yourDatabase;uid=sa;pwd=;"
   cn.Open
   rs.ActiveConnection = cn        
   rs.Properties("Server Data On Insert").Value = True
   rs.Open "SELECT * FROM Employee", , adOpenKeyset, adLockOptimistic
   rs.AddNew
   rs.Fields(1) = "Test"
   rs.Update
   ' Here should be the correct identity value inserted into Employee Table
instead of Audit Table        

   Debug.Print rs.Fields(0)
   rs.Close
   cn.Close

        For more information, please read the following article:
        Q219029 HOWTO: Retrieving Calculated Fields from SQL Server 7.0
        http://support.microsoft.com/support/kb/articles/q219/0/29.asp

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.
Copy Right, 2001 Microsoft Corporation. All rights reserved.

Regards,
Kally Wang
Microsoft Support Engineer


Subject: Getting the identity value of a newly inserted row
Date: Tue, 16 Oct 2001 16:00:14 -0400
Newsgroups: microsoft.public.vb.database.ado

Hi,

I have a question regarding identity columns in SQL Server 7.

Sometimes, after inserting a row into a table using ADO, you would want to
get the identity created for the newly inserted row. For instance, if table
EMPLOYEE has an identity column called EmployeeId, after calling
Recordset.Update, you can read Recordset("EmployeeId").Value and get the
identity value assigned to that newly inserted row. This works okay unless
EMPLOYEE table has an insert-trigger that inserts rows to some other table.
Say, after inserting a new EMPLOYEE row, you must add into the AUDIT table
that "employee so and so has been added." , and the primary key for the
AUDIT table is another identity column (called AuditID). If EMPLOYEE table
has an insert-trigger as described above, after calling Recordset.Update
method, what you read from Recordset("EmployeeID").Value is the number that
was auto-generated for the identity column of AUDIT table, and not the
number that was auto-generated for the identity column of EMPLOYEE table. I

was added, the identity of the AUDIT table was the one that was returned
instead.

So, me and my comworker think that an alternative solution to reading the
identity value after a recordset update is by doing something like "Select
Max(EmployeeID) From EMPLOYEE". This strategy looks like it's going to work
90% of the time. But our concern is, if the database is busy (many
concurrent users doing concurrent inserts on the same tables), the "Select
Max(IdentityColumn)" strategy might not work.

I guess my main question is: Is there an efficient, bullet-proof way of
retrieving the newly created identity column value for a row that was just
inserted into a table that fires a trigger which in turn inserts a row into
another table that also has an identity column?

Any tips, recommendations would be greatly appreciated. Thanks!

-Gabe



Tue, 06 Apr 2004 13:48:42 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Getting IDENTITY value of newly inserted row

2. Identity cannot be determined for newly inserted rows

3. Error - The provider is unable to determine identity for newly inserted rows (0x80040E1B)

4. Obtaining the value of the Identity column of a newly inserted record

5. How to retrieve the IDENTITY (Counter) value of INSERTed rows

6. Getting the value of an autoincrement or identity field during an sql insert

7. Getting back an identity field value after an insert SQL statement

8. Getting the value of an autoincrement or identity field during an sql insert

9. How do I get the AutoNumber (or Identity) for a newly inserted record (from able-consulting)

10. VB5, SQL6.5: row identity after INSERT?

11. Insert vs AddNew - Getting Identity

12. Insert vs AddNew - Getting Identity

 

 
Powered by phpBB® Forum Software