Hello,
I am doing Insert using SqlTransaction but I need to get primary key of last Inserted table to use it in next statement. I don't want to commit the transaction before complete insert in second table. so how to get primary key in SQLTransaction before commit it
Thanks


Munesh SharmaRank #174 · 13.9K reputationPosted Apr 12, 2014, 4:24 AM EDT
Use SCOPE_IDENTITY() just after the INSERT
--
DECLARE @newId INT
INSERT INTO MyTable1(
Field1
)
SELECT @MyVal1
--End of Insert here
SELECT @newId = SCOPE_IDENTITY()
--Rest of the procedure
Ajay Patel148 reputationPosted Apr 22, 2014, 2:29 AM EDT
SELECT IDENT_CURRENT ('table Name') AS Current_Identity;