27

I want to retrieve the id of a newly inserted record with an auto incrementing id column (using the sequence and trigger method). What is the standard way to do this?

haymansfield
  • 5,419
  • 4
  • 34
  • 51

3 Answers3

38

Use the PL/SQL RETURNING clause:

insert into mytable (...) values (...)
returning id into v_id;
Tony Andrews
  • 129,880
  • 21
  • 220
  • 259
0

use the results.lastRowid to make a query on the same table like "SELECT * FROM YOUR_TABLE WHERE ROWID = :lastRowid"

lastRowid looks like "AAAS/tAAFAAAA/QAAB"

Jobin
  • 79
  • 3
  • 8
0

You need to use a bind to recovery the value.

. . .
const result = await connection.execute(
    `INSERT INTO mytable (mydata) VALUES ('Hello') RETURN myid INTO :id`,
    {id : {type: oracledb.NUMBER, dir: oracledb.BIND_OUT } }
);

console.log(result.outBinds.id);  // print the ID of the inserted row

https://node-oracledb.readthedocs.io/en/latest/user_guide/sql_execution.html#getting-the-last-insert-id