This is a blog where you are going to find tips or solution that are going to be helpful in the technology environment.
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
Labels:
T-SQL
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment