Click here to Skip to main content
15,899,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to return primary key after inserting in sql server 2005 in insert query?.please help me about this question.
Posted

A single insert statement will automatically return the last inserted identity value.

You can use the following also :
SQL
select @@IDENTITY
 
Share this answer
 
Comments
krupalmehta 20-Sep-11 5:14am    
i have insert query like..
INSERT INTO ProductImage
(ProductId, ImageTitle, ImageUrl, IsVisible, IsCoverImage)
VALUES (@ProductId,@ImageTitle,@ImageUrl,@IsVisible,@IsCoverImage)
so please explain how to write select @@identity
Mehdi Gholam 20-Sep-11 5:22am    
In C# int i = ExecuteInsert(...); in sql insert... select @@identity
nagendrathecoder 20-Sep-11 5:20am    
good answer
select @@IDENTITY;


More info here[^].
 
Share this answer
 
Comments
nagendrathecoder 20-Sep-11 5:20am    
good answer
krupalmehta 20-Sep-11 5:22am    
Not work
Toniyo Jackson 20-Sep-11 5:25am    
After your insert query write select @@identity;
Define the out parameter with a stored procedure. Read the following,

http://msdn.microsoft.com/en-us/library/ks9f57t0%28vs.71%29.aspx
 
Share this answer
 
v2
Comments
krupalmehta 20-Sep-11 5:12am    
how to in sql server
CodingLover 20-Sep-11 5:15am    
Read the link I have added above.

And also you can use "Select Max(pk_id) From Table1" format.

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