How to get the currently inserted row identity through query in sqlserver ?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit ChoudharyRank #145 · 15.8K reputationPosted Jul 4, 2011, 2:20 AM EDT
Thanks.
Vilas GiteRank #951 · 2.1K reputationPosted Jul 4, 2011, 3:06 AM EDT
if your using SQL Server 2005, let's use @@identity.
------------------------------------------------------
USE tour;
GO
--Display the value of tour_name in the last row in the table.
SELECT MAX(tour_name) FROM tourtbl;
GO
INSERT INTO tourtbl (tour_name,min_people,max_people,unit_price)
VALUES ('UK','5','5','25000');
GO
SELECT @@IDENTITY AS 'Identity';
GO
--Display the value of tour_name of the newly inserted row.
SELECT MAX(tour_name) FROM tourtbl;
GO
----------------------------------------------------------------------------