Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

How to get the Primary Key after Insertion in MSSQL

Thanks

Dileep
Posted

If primary key column is identity column, then use

SCOPE_IDENTITY()

Or if you know the primary key column name, then
like this,

SQL
insert into tableTest(testUserEmail,testUserName)
output inserted.ID
values (@testValue, @testName)




Here ID is my primary key column name
 
Share this answer
 
Comments
dilzz 21-Aug-12 1:34am    
Thanks 4 ur reply...
use
VB
SCOPE_IDENTITY()
 
Share this answer
 
HI,

I got the Solution

Here is the below sample Code,

SQL
INSERT INTO tblUser_Master (User_Name,Password,Active_Status) VALUES ('Man','123',1);
GO
SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY];
GO


Thanks

Dileep
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900