Wednesday, June 11, 2014

How to get the last ID inserted in SQL


To get the last ID after an insertion you can use the @@Identity system function, if there was not insertion you will get a NULL value:

INSERT INTO People(First) VALUES ('Joseph');
SELECT ID FROM People WHERE ID = @@Identity;


You can also use the MAX function to get the last record inserted:

SELECT MAX(ID) AS ID FROM People

Another way to get it, is by the ident_current function:

SELECT ident_current(People) AS ID

No comments:

Post a Comment