Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining the PK value of a new record

Status
Not open for further replies.

jalbao

Programmer
Nov 27, 2000
413
US
i'm using sql server 2k


after i insert a record into my table, how can i retrieve the value of the new pk value (identity) that sql sever generated. ideally, i would like to accomplish this without having to use another query [example select Max(pk)]

tia
 
SELECT @@IDENTITY immediately after your INSERT statement.

Put both statements in a transaction.
 
Just a little warning about triggers:
If you insert to a table and generate a new Identity, but that insert fires a trigger that makes another insert on another table with an identity, the @@IDENTITY value you get back right after the first insert will actually be from the insert fired by the trigger.
 
thx glitchbane - i'll keep that in mind
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top