Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Get PK after Insert
Message
 
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Title:
Get PK after Insert
Miscellaneous
Thread ID:
01675696
Message ID:
01675696
Views:
89
Hi,

I am sure I have done it before but forgot how to do it. In a Stored Procedure, the code Inserts a row into a table.
How do I get the PK (Identity) of the newly added row?
Also, is it after COMMIT or before?

TIA

UPDATE. I code I found is as follows (right after INSERT)
Set @NewPk = SCOPE_IDENTITY()
However, the stored procedure does two tables INSERT. If I do the SCOPE_IDENTITY() right after the table where I need the Identity, will it work?
Google shows two more approaches:
@@Identity.
Ident_Current()
But which one to choose?
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Next
Reply
Map
View

Click here to load this message in the networking platform