Get last entered id

Is there a way to get the last inserted id if I use SQL Server CE? I have 2 tables, and when a new record is created, I also want to save the identifier in the second table.

+4
source share
5 answers

I hope this example helps you.

INSERT INTO jobs (job_desc,min_level,max_level) VALUES ('A new job', 25, 100); SELECT job_id FROM jobs WHERE job_id = @@IDENTITY; 
+5
source
 SELECT @@IDENTITY 

will retrieve the last automatically generated authentication value in SQL Server.

+12
source

Assuming higher id values ​​are always newer, how about:

 SELECT TOP 1 id FROM your_table ORDER BY id DESC 

or

 SELECT MAX(id) FROM your_table 
+2
source

use this query:

 INSERT INTO Persons (FirstName) VALUES ('Joe'); SELECT ID AS LastID FROM Persons WHERE ID = @@Identity; 

or you can view this link for more information: http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of -record /

+1
source

It worked great for me

 SELECT TOP (1) your_id FROM your_table ORDER BY your_id DESC 
-1
source

Source: https://habr.com/ru/post/1391528/


All Articles