Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return last inserted ID/SERIAL values with CockroachDB

Tags:

cockroachdb

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()?

like image 511
jseldess Avatar asked Apr 12 '17 02:04

jseldess


1 Answers

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;
like image 129
jseldess Avatar answered Nov 19 '22 04:11

jseldess