Skip to content

Commit 8014f63

Browse files
author
Christopher Jones
committed
Add lastRowid example
1 parent 74d074f commit 8014f63

File tree

5 files changed

+25
-29
lines changed

5 files changed

+25
-29
lines changed

doc/api.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3862,8 +3862,11 @@ readonly String lastRowid
38623862
```
38633863

38643864
The ROWID of a row affected by an INSERT, UPDATE, DELETE or MERGE statement.
3865-
For other statements, or if no row was affected, it is not set. If more than
3866-
one row was affected, only the ROWID of the last row is returned.
3865+
For other statements, or if no row was affected, it is not set.
3866+
3867+
If more than one row was affected, only the ROWID of the last row is returned.
3868+
To get all ROWIDs of multiple rows see [DML RETURNING Bind
3869+
Parameters](#dmlreturn).
38673870

38683871
This property was added in node-oracledb 4.2.
38693872

@@ -11694,6 +11697,9 @@ const result = await connection.execute(
1169411697
console.log(result.outBinds.id); // print the ID of the inserted row
1169511698
```
1169611699

11700+
Instead of using application generated identifiers, you may prefer to use
11701+
ROWIDs, see [`lastRowid`](#execlastrowid).
11702+
1169711703
### <a name="cursors1000"></a> 16.2 Cursor Management
1169811704

1169911705
A cursor is a "handle for the session-specific private SQL area that holds a

examples/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ File Name | Description
5858
[`dbmsoutputpipe.js`](dbmsoutputpipe.js) | Show fetching DBMS_OUTPUT by using a pipelined table
5959
[`demodrop.js`](demodrop.js) | Drops the schema objects created by the examples
6060
[`demosetup.js`](demosetup.js) | Used to create common schema objects for the examples
61-
[`dmlrupd1.js`](dmlrupd1.js) | Example of DML RETURNING with a single row match
62-
[`dmlrupd2.js`](dmlrupd2.js) | Example of DML RETURNING where multiple rows are matched
61+
[`dmlrupd.js`](dmlrupd.js) | Example of DML RETURNING where multiple rows are matched
6362
[`em_batcherrors.js`](em_batcherrors.js) | `executeMany()` example showing handling data errors
6463
[`em_dmlreturn1.js`](em_dmlreturn1.js) | `executeMany()` example of DML RETURNING that returns single values
6564
[`em_dmlreturn2.js`](em_dmlreturn2.js) | `executeMany()` example of DML RETURNING that returns multiple values
@@ -73,6 +72,7 @@ File Name | Description
7372
[`impres.js`](impres.js) | Shows PL/SQL 'Implict Results' returning multiple query results from PL/SQL code.
7473
[`insert1.js`](insert1.js) | Basic example creating a table and inserting data. Shows DDL and DML
7574
[`insert2.js`](insert2.js) | Basic example showing auto commit behavior
75+
[`lastinsertid.js`](lastinsertid.js) | Shows inserting a row and getting its ROWID.
7676
[`lobbinds.js`](lobbinds.js) | Demonstrates how to bind and query LOBs
7777
[`lobinsert1.js`](lobinsert1.js) | Shows inserting a file into a CLOB column
7878
[`lobinsert2.js`](lobinsert2.js) | Inserts text into a CLOB column using the RETURNING INTO method.

examples/demodrop.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ async function run() {
6363

6464
`DROP TABLE no_purchaseorder_b PURGE`,
6565

66+
`DROP TABLE no_lastinsertid PURGE`,
67+
6668
`DROP TABLE no_dmlrupdtab PURGE`,
6769

6870
`DROP TABLE no_lobs PURGE`,

examples/dmlrupd2.js renamed to examples/dmlrupd.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
* limitations under the License.
1717
*
1818
* NAME
19-
* dmlrupd2.js
19+
* dmlrupd.js
2020
*
2121
* DESCRIPTION
2222
* Example of 'DML Returning' with multiple rows matched.
2323
* The ROWIDs of the changed records are returned. This is how to get
24-
* the 'last insert id'.
24+
* the 'last insert id' of multiple rows. For a single row, use "lastRowid".
2525
*
2626
* This example uses Node 8's async/await syntax.
2727
*

examples/dmlrupd1.js renamed to examples/lastinsertid.js

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved. */
1+
/* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. */
22

33
/******************************************************************************
44
*
@@ -16,12 +16,11 @@
1616
* limitations under the License.
1717
*
1818
* NAME
19-
* dmlrupd1.js
19+
* lastinsertid.js
2020
*
2121
* DESCRIPTION
22-
* Example of 'DML Returning' with a single row match.
23-
* The ROWID of the changed record is returned. This is how to get
24-
* the 'last insert id'.
22+
* Example of inserting a row and getting it's ROWID.
23+
* To return application generated identifiers, see dmlrupd.js.
2524
*
2625
* This example uses Node 8's async/await syntax.
2726
*
@@ -53,13 +52,9 @@ async function run() {
5352
//
5453

5554
const stmts = [
56-
`DROP TABLE no_dmlrupdtab`,
55+
`DROP TABLE no_lastinsertid`,
5756

58-
`CREATE TABLE no_dmlrupdtab (id NUMBER, name VARCHAR2(40))`,
59-
60-
`INSERT INTO no_dmlrupdtab VALUES (1001, 'Venkat')`,
61-
62-
`INSERT INTO no_dmlrupdtab VALUES (1002, 'Neeharika')`
57+
`CREATE TABLE no_lastinsertid (id NUMBER, name VARCHAR2(40))`,
6358
];
6459

6560
for (const s of stmts) {
@@ -72,28 +67,21 @@ async function run() {
7267
}
7368

7469
//
75-
// Show DML Returning
70+
// Execute the SQL statement
7671
//
7772

78-
// SQL statement.
79-
// Note bind names cannot be reused in the DML section and the RETURNING section
80-
const sql =
81-
`UPDATE no_dmlrupdtab
82-
SET name = :name
83-
WHERE id = :id
84-
RETURNING ROWID INTO :rid`;
73+
const sql = `INSERT INTO no_lastinsertid VALUES (:id, :name)`;
8574

8675
const result = await connection.execute(
8776
sql,
8877
{
89-
id: 1001,
90-
name: "Krishna",
91-
rid: { type: oracledb.STRING, dir: oracledb.BIND_OUT }
78+
id: 1000,
79+
name: "Chris"
9280
},
9381
{ autoCommit: true }
9482
);
9583

96-
console.log(result.outBinds);
84+
console.log("The ROWID is", result.lastRowid);
9785

9886
} catch (err) {
9987
console.error(err);

0 commit comments

Comments
 (0)