Skip to content

Commit f3d26f0

Browse files
committed
Add doc for PL/SQL Index-by array Bind by Position syntax
1 parent 7439a6a commit f3d26f0

File tree

1 file changed

+84
-73
lines changed

1 file changed

+84
-73
lines changed

doc/api.md

Lines changed: 84 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3416,20 +3416,80 @@ binding can be a very efficient way of transferring small data sets.
34163416
Note PL/SQL's `VARRAY` and nested table collection types cannot be
34173417
bound.
34183418
3419-
To bind arrays use the named bind syntax:
3419+
Given this table and PL/SQL package:
3420+
3421+
```sql
3422+
DROP TABLE mytab;
3423+
CREATE TABLE mytab (id NUMBER, numcol NUMBER);
3424+
3425+
CREATE OR REPLACE PACKAGE mypkg IS
3426+
TYPE numtype IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
3427+
PROCEDURE myinproc(p_id IN NUMBER, vals IN numtype);
3428+
PROCEDURE myoutproc(p_id IN NUMBER, vals OUT numtype);
3429+
END;
3430+
/
3431+
3432+
CREATE OR REPLACE PACKAGE BODY mypkg IS
3433+
3434+
PROCEDURE myinproc(p_id IN NUMBER, vals IN numtype) IS
3435+
BEGIN
3436+
FORALL i IN INDICES OF vals
3437+
INSERT INTO mytab (id, numcol) VALUES (p_id, vals(i));
3438+
END;
3439+
3440+
PROCEDURE myoutproc(p_id IN NUMBER, vals OUT numtype) IS
3441+
BEGIN
3442+
SELECT numcol BULK COLLECT INTO vals FROM mytab WHERE id = p_id ORDER BY 1;
3443+
END;
3444+
3445+
END;
3446+
/
3447+
```
3448+
3449+
To bind an array in node-oracledb using "bind by name" syntax for insertion into `mytab` use:
34203450
34213451
```javascript
34223452
connection.execute(
3423-
"BEGIN mypkg.myinproc(:bv); END;",
3453+
"BEGIN mypkg.myinproc(:id, :vals); END;",
34243454
{
3425-
bv: { type: oracledb.NUMBER,
3426-
dir: oracledb.BIND_IN,
3427-
val: [1, 2, 23, 4, 10]
3428-
}
3455+
id: 1234,
3456+
vals: { type: oracledb.NUMBER,
3457+
dir: oracledb.BIND_IN,
3458+
val: [1, 2, 23, 4, 10]
3459+
}
34293460
}, . . .
34303461
```
34313462
3432-
Positional bind syntax is not supported in this release.
3463+
Alternatively, "bind by position" syntax can be used:
3464+
3465+
```javascript
3466+
connection.execute(
3467+
"BEGIN mypkg.myinproc(:id, :vals); END;",
3468+
[
3469+
1234,
3470+
{ type: oracledb.NUMBER,
3471+
dir: oracledb.BIND_IN,
3472+
val: [1, 2, 23, 4, 10]
3473+
}
3474+
],
3475+
3476+
function (err) { . . . });
3477+
```
3478+
3479+
After executing either of these `mytab` will contain:
3480+
3481+
```
3482+
ID NUMCOL
3483+
---------- ----------
3484+
1234 1
3485+
1234 2
3486+
1234 23
3487+
1234 4
3488+
1234 10
3489+
```
3490+
3491+
The [`type`](#executebindParams) must be set for PL/SQL array binds.
3492+
It can be set to `STRING` or `NUMBER`
34333493
34343494
For OUT and IN OUT binds, the [`maxArraySize`](#executebindParams)
34353495
bind property must be set. Its value is the maximum number of
@@ -3453,86 +3513,32 @@ value is not large enough to hold the longest string data item in the
34533513
collection a runtime error occurs. To avoid unnecessary memory
34543514
allocation, do not let the size be larger than needed.
34553515
3456-
See
3457-
[plsqlarray.js](https://github.com/oracle/node-oracledb/tree/master/examples/plsqlarray.js)
3458-
for a full example.
3459-
3460-
The following example passes an array of values to a PL/SQL procedure
3461-
which inserts them into a table. The procedure is defined as:
3462-
3463-
```sql
3464-
CREATE TABLE mytab (numcol NUMBER);
3465-
3466-
CREATE OR REPLACE PACKAGE mypkg IS
3467-
TYPE numtype IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
3468-
PROCEDURE myinproc(p IN numtype);
3469-
END;
3470-
/
3471-
3472-
CREATE OR REPLACE PACKAGE BODY mypkg IS
3473-
PROCEDURE myinproc(p IN numtype) IS
3474-
BEGIN
3475-
FORALL i IN INDICES OF p
3476-
INSERT INTO mytab (numcol) VALUES (p(i));
3477-
END;
3478-
END;
3479-
/
3480-
```
3481-
3482-
With this, the following JavaScript will result in `mytab` containing
3483-
one row per value:
3484-
3485-
```javascript
3486-
connection.execute(
3487-
"BEGIN mypkg.myinproc(:bv); END;",
3488-
{
3489-
bv: { type: oracledb.NUMBER,
3490-
dir: oracledb.BIND_IN,
3491-
val: [1, 2, 23, 4, 10]
3492-
}
3493-
},
3494-
function (err) { . . . });
3495-
```
3496-
3497-
The next example fetches an array of values from a table:
3516+
The next example fetches an array of values from a table. First,
3517+
insert these values:
34983518
34993519
```sql
3500-
CREATE TABLE mytab (numcol NUMBER);
3501-
INSERT INTO mytable (numcol) VALUES (10);
3502-
INSERT INTO mytable (numcol) VALUES (25);
3503-
INSERT INTO mytable (numcol) VALUES (50);
3520+
INSERT INTO mytab (id, numcol) VALUES (99, 10);
3521+
INSERT INTO mytab (id, numcol) VALUES (99, 25);
3522+
INSERT INTO mytab (id, numcol) VALUES (99, 50);
35043523
COMMIT;
3505-
3506-
CREATE OR REPLACE PACKAGE mypkg IS
3507-
TYPE numtype IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
3508-
PROCEDURE myoutproc(p OUT numtype);
3509-
END;
3510-
/
3511-
3512-
CREATE OR REPLACE PACKAGE BODY mypkg IS
3513-
PROCEDURE myoutproc(p OUT numtype) IS
3514-
BEGIN
3515-
SELECT numcol BULK COLLECT INTO p FROM mytab ORDER BY 1;
3516-
END;
3517-
END;
3518-
/
35193524
```
35203525
3521-
With this table and package, the following JavaScript will print
3526+
With these values, the following node-oracledb code will print
35223527
`[ 10, 25, 50 ]`.
35233528
35243529
```javascript
35253530
connection.execute(
3526-
"BEGIN mypkg.myoutproc(:bv); END;",
3531+
"BEGIN mypkg.myoutproc(:id, :vals); END;",
35273532
{
3528-
bv: { type: oracledb.NUMBER,
3529-
dir: oracledb.BIND_OUT,
3530-
maxArraySize: 10 // allocate memory to hold 10 numbers
3533+
id: 99,
3534+
vals: { type: oracledb.NUMBER,
3535+
dir: oracledb.BIND_OUT,
3536+
maxArraySize: 10 // allocate memory to hold 10 numbers
35313537
}
35323538
},
35333539
function (err, result) {
35343540
if (err) { console.error(err.message); return; }
3535-
console.log(result.outBinds.bv);
3541+
console.log(result.outBinds.vals);
35363542
});
35373543
```
35383544
@@ -3546,6 +3552,11 @@ See [Oracledb Constants](#oracledbconstants) and
35463552
[execute(): Bind Parameters](#executebindParams) for more information
35473553
about binding.
35483554
3555+
See
3556+
[plsqlarray.js](https://github.com/oracle/node-oracledb/tree/master/examples/plsqlarray.js)
3557+
for a runnable example.
3558+
3559+
35493560
## <a name="transactionmgt"></a> 13. Transaction Management
35503561
35513562
By default,

0 commit comments

Comments
 (0)