SAMPLE: Ident.exe Retrieves @@IDENTITY From ODBC Inserts

ID: Q185821


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.

The Ident.exe sample demonstrates how to retrieve the automatic @@IDENTITY value from an insert into a Microsoft SQL Server database. The sample consists of a simple MFC AppWizard project based on the CRecordset class. The project was modified slightly to allow inserts into the table and to retrieve the automatically generated identity value for the insert. It uses the pubs data source name (DSN), although any DSN will work, and a table called tblIdentity.


MORE INFORMATION

The following file is available for download from the Microsoft Software Library:

~ Ident.exe
Release Date: May-19-1998

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
Q119591 How to Obtain Microsoft Support Files from Online Services
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 Description

A 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:

The following SQL script, located in the Master.sql file, generates the sample table, insertTbl:

   CREATE TABLE insertTbl (myAuto int IDENTITY (1, 1) NOT NULL ,
                           lname varchar (15) NOT NULL ,
                           fname varchar (15) NOT NULL ) 
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:

   CREATE TRIGGER trgAudit ON insertTbl FOR INSERT
      AS
      INSERT INTO tblIdentity VALUES (@@IDENTITY, 'insertTbl', @@SPID) 
Immediately after an insertion into tblIdentity, the trigger fires and stores:

After insertion, the program calls a stored procedure (called sp_getID) to retrieve the identity value.

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 Sample

To 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:

   insert into insertTbl (lname,fname) VALUES ('Smith','Joe')
   select * from tblIdentity 
The Results tab should look similar to the following:

   iID         strTable        SPID
   ----------- --------------- -----------
   5           insertTbl       11

   (1 row(s) affected) 
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.


REFERENCES

For 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 
Version           : WINDOWS:3.5,3.6; WINNT:5.0
Platform          : WINDOWS winnt 
Issue type        : kbhowto 

Last Reviewed: July 27, 1999