In SQL Server 2005, I insert a row into a table using a stored procedure, and I want to get the new primary key value right after inserting this row. I use the following approach to get the primary key value after the insert line
Create Proc Sp_Test @testEmail varchar(20)=null,-- Should be Unique @testName varchar(20)=null -- Should be Unique as begin insert into tableTest (testUserEmail,testUserName)values (@testValue,@testName) select MAX(ID) from tableTest --ID is Primary Key
- or
select ID from tableTest where testUserEmail =@testValue and testUserName = @testName
- or
select SCOPE_IDENTITY() as ID end
Please suggest me which approach is best to accomplish the described task.
Shailesh sahu
source share