How do I get the last ID/SERIAL value inserted into a table? Is there a CockroachDB function similar to SQL Server’s SCOPE_IDENTITY()
?
Asked
Active
Viewed 671 times
5

jseldess
- 373
- 1
- 6
1 Answers
7
There’s no function in CockroachDB for returning last inserted values. But you can use the RETURNING
clause of the INSERT
statement.
For example, this is how you’d use RETURNING
to return an auto-generated SERIAL value:
CREATE TABLE users (id SERIAL, name STRING);
INSERT INTO users (name) VALUES ('mike') RETURNING id;

Jordan Lewis
- 16,900
- 4
- 29
- 46

jseldess
- 373
- 1
- 6