Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle get id of inserted row with identity always [duplicate]

Currently I have a table with this structure:

CREATE TABLE "DUMMY_SCHEMA"."NAMES"
(
  "ID" NUMBER(10,0) GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1 CACHE 20) NOT NULL 
, "NAME" NVARCHAR2(1024) NOT NULL 
, CONSTRAINT "NAMES_PK" PRIMARY KEY ("ID")
);

In SQL Server I only need to do following to get the Id of the inserted row.

INSERT INTO [NAMES]([NAME])VALUES('Random'); SELECT SCOPE_IDENTITY() Id

What would be the equivalent for Oracle 12c?

like image 408
AceVentur4 Avatar asked Mar 06 '23 22:03

AceVentur4


1 Answers

The equivalent is

INSERT INTO dummy_schema.names (name) VALUES ('Random') 
RETURNING id INTO :myvalue;

The mechanism how to pick up the returned ID depends on the host language (Java, PL/SQL, SQL*Plus etc).

like image 195
wolφi Avatar answered Mar 08 '23 22:03

wolφi