简体   繁体   中英

Get last inserted id

Is there any way I can get the last inserted ID if I am using SQL Server CE? I have 2 tables and when a new record is created, I want to be able to save the ID in the second table too.

SELECT @@IDENTITY

将检索SQL Server中最后一个自动生成的标识值。

I hope this example will help 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;

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

SELECT TOP 1 id FROM your_table ORDER BY id DESC

or:

SELECT MAX(id) FROM your_table

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 info: http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/

这对我来说很好

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM