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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting ID after INSERT statement.

Status
Not open for further replies.

sjuarez1979

Programmer
Jun 26, 2001
35
US
I am creating a stored procedure that Inserts data into one table and using the new ID of the previously inserted record, Inserts data into another table. How is this acheived most efficiently? The ID of the first inserted set of data is server generated using autonumber.
Thanks for the help!
-sjuarez1979
 
After each insert you do a Select @@IDENTITY, which returns you the number that was assigned.

Chip H.
 
You can get the last number inserted using @@Identity.

Declare @id Int

Insert Table...

--Load @id with the last inserted identity value.
Select @id=@@identity Terry L. Broadbent - Salt Lake City, UT
Home of the 2002 Winter Olympics (Feb 8-24)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top