The information in this article applies to:
SUMMARY
When inserting values into a table with an identity column, SQL Server
automatically generates the next identifier based on the last used identity
value. You might want to retrieve this automatically generated identity
value immediately after an insert.
MORE INFORMATIONThe following files are available for download from the Microsoft
Download Center. Click the file names below to download the files: http://www.microsoft.com/downloads/search.aspand then click How to use the Microsoft Download Center. Ident.exe includes an algorithm and sample code that illustrate how to retrieve the identity value from a SQL Server database. You can use this method for multiple processes or threads doing simultaneous inserts into the same table. (Note that threads cannot share connections.) Project DescriptionA master identity table, called tblIdentity, is created. It consists of the identity, table name, and server process ID number (SPID). An insert trigger into tblIdentity is created for each table in the database that satisfies the following criteria:
Because names are not unique, the code uses the identity property of SQL
Server to automatically generate a unique key.
The following SQL script, located in the Master.sql file, creates an insert trigger for insertTbl:
Immediately after an insertion into tblIdentity, the trigger fires and
stores:
The code calls the stored procedure sp_getID (with a negative value for the spid parameter) in the one time initialization to get the SPID (the server process ID number of the current process, guaranteed to be unique for each connection). Applications with multiple threads inserting into the same table must not share a connection. Each thread must have it's own connection to guarantee a unique SPID. NOTE: applications with multiple threads sharing a connection must use a more complicated algorithm that includes the thread ID and the SPID to establish a unique insertion. After each insertion, the program calls sp_getID and passes the table name where the insert occurred and the SPID that was retrieved in the initial call to sp_getID. The stored procedure sp_getID returns the unique identity value that SQL Server generated on the insert. Running the SampleTo use the sample application, run the Master.sql file in the SQL Query Tool from SQL Enterprise Manager. After running the script, you can verify that the tables and triggers were created successfully by running the following SQL statement:
The Results tab should look similar to the following:
The iID and SPID values you see will probably not be 5 and 11. The
tblIdentity values verify that the Master.sql script was successful.
Note: The sample code uses a sub-optimal method to display the identity value and is used for illustration purposes only. The sample uses the SQLExecDirect function to run the stored procedure and retrieve the identity value. REFERENCESFor more information, please see the following article in the Microsoft Knowledge Base: Q163446 PRB: Guarantee @@IDENTITY Value on a Per Table Basis Additional query words: kbSQLServ kbSQL kbDriver kbODBC kbMFC kbVC500 kbDSupport kbdse
Keywords : kbfile kbsample |
Last Reviewed: December 11, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |