// system & session reference

SQL LAST_INSERT_ID and SCOPE_IDENTITY

After an INSERT into an auto-numbered table you usually need the new key. Each engine keeps it per connection, and RETURNING or OUTPUT hands it back from the INSERT itself.

カテゴリ
System & session
Returns
the generated key (numeric)
In this category
1 of 13

構文

SCOPE_IDENTITY()
LAST_INSERT_ID()
last_insert_rowid()
INSERT ... RETURNING id

Each block is a separate form. The support table and the examples show which engine takes which.

エンジンサポート

エンジンSupportメモ
SQL Server はい SCOPE_IDENTITY(); OUTPUT inserted.id returns every new key
PostgreSQL Different name or syntax INSERT ... RETURNING id; lastval() or currval('seq') afterwards
MySQL はい LAST_INSERT_ID()
SQLite Different name or syntax last_insert_rowid(); RETURNING from 3.35
Oracle Different name or syntax INSERT ... RETURNING id INTO a PL/SQL variable or bind

例

SQL Server

CREATE TABLE notes (id INT IDENTITY PRIMARY KEY, body VARCHAR(50));
INSERT INTO notes (body) VALUES ('a'), ('b'), ('c');
SELECT SCOPE_IDENTITY() AS new_id;  -- 3

MySQL

CREATE TABLE notes (id INT AUTO_INCREMENT PRIMARY KEY, body VARCHAR(50));
INSERT INTO notes (body) VALUES ('a'), ('b'), ('c');
SELECT LAST_INSERT_ID() AS new_id;  -- 1, the first row of the statement

PostgreSQL

CREATE TABLE notes (id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, body TEXT);
INSERT INTO notes (body) VALUES ('a'), ('b') RETURNING id;  -- 1, 2

SQLite

CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT);
INSERT INTO notes (body) VALUES ('a'), ('b');
SELECT last_insert_rowid() AS new_id;  -- 2

知っておくと便利なこと

  • SQL Server's @@IDENTITY returns the last identity value the session generated in any scope, including a trigger that wrote to an audit table. SCOPE_IDENTITY() stays in the current scope, and IDENT_CURRENT('table') ignores the session altogether.
  • After a multi-row INSERT, MySQL's LAST_INSERT_ID() is the id of the first row in that statement. SQL Server's SCOPE_IDENTITY() is the last one.
  • SELECT MAX(id) races with other sessions and can return someone else's row. The functions here are per connection, so they are safe only when the INSERT and the read run on the same connection.
  • When one INSERT adds several rows, return the keys from the statement: OUTPUT inserted.id on SQL Server, RETURNING id on PostgreSQL and SQLite.

Questions about LAST_INSERT_ID / SCOPE_IDENTITY

Is SCOPE_IDENTITY safe with concurrent inserts?

Yes. It reads the value your own session generated in the current scope, so another connection inserting at the same moment does not change it. A later insert into another identity table in the same scope does.

How do I get every id from a multi-row insert?

Return them from the INSERT: OUTPUT inserted.id on SQL Server, RETURNING id on PostgreSQL and SQLite, RETURNING ... BULK COLLECT INTO in Oracle PL/SQL. MySQL has no RETURNING, so insert one row at a time or look the rows up again by a natural key.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.