@@ -112,6 +112,7 @@ limitations under the License.
112
112
- 10.1 [ PL/SQL Stored Procedures] ( #plsqlproc )
113
113
- 10.2 [ PL/SQL Stored Functions] ( #plsqlfunc )
114
114
- 10.3 [ Anonymous PL/SQL blocks] ( #plsqlanon )
115
+ - 10.4 [ Using DBMS_OUTPUT] ( #dbmsoutput )
115
116
11 . [ Working with CLOB and BLOB Data] ( #lobhandling )
116
117
12 . [ Bind Parameters for Prepared Statements] ( #bind )
117
118
- 12.1 [ IN Bind Parameters] ( #inbind )
@@ -2494,6 +2495,84 @@ The output is:
2494
2495
2495
2496
See [Bind Parameters for Prepared Statements](#bind) for information on binding.
2496
2497
2498
+ ### <a name="dbmsoutput"></a> 10.4 Using DBMS_OUTPUT
2499
+
2500
+ The
2501
+ [DBMS_OUTPUT](http://docs.oracle.com/database/121/ARPLS/d_output.htm#ARPLS036)
2502
+ package is the standard way to "print" output from PL/SQL. The way
2503
+ DBMS_OUTPUT works is like a buffer. Your Node.js application code
2504
+ must first turn on DBMS_OUTPUT buffering for the current connection by
2505
+ calling the PL/SQL procedure ` DBMS_OUTPUT .ENABLE (NULL )` . Then any
2506
+ PL/SQL executed by the connection can put text into the buffer using
2507
+ ` DBMS_OUTPUT .PUT_LINE ()` . Finally ` DBMS_OUTPUT .GET_LINE ()` is used to
2508
+ fetch from that buffer. Note, any PL/SQL code that uses DBMS_OUTPUT
2509
+ runs to completion before any output is available to the user. Also,
2510
+ other database connections cannot access your buffer.
2511
+
2512
+ A basic way to fetch DBMS_OUTPUT with node-oracledb is to bind an
2513
+ output string when calling the PL/SQL ` DBMS_OUTPUT .GET_LINE ()`
2514
+ procedure, print the string, and then repeat until there is no more
2515
+ data. The following snippet is based on the example
2516
+ [dbmsoutputgetline.js](https://github.com/oracle/node-oracledb/tree/master/examples/dbmsoutputgetline.js):
2517
+
2518
+ ` ` ` javascript
2519
+ function fetchDbmsOutputLine (connection , cb ) {
2520
+ connection .execute (
2521
+ " BEGIN DBMS_OUTPUT.GET_LINE(:ln, :st); END;" ,
2522
+ { ln: { dir: oracledb .BIND_OUT , type: oracledb .STRING , maxSize: 32767 },
2523
+ st: { dir: oracledb .BIND_OUT , type: oracledb .NUMBER } },
2524
+ function (err , result ) {
2525
+ if (err) {
2526
+ return cb (err, connection);
2527
+ } else if (result .outBinds .st == 1 ) { // no more output
2528
+ return cb (null , connection);
2529
+ } else {
2530
+ console .log (result .outBinds .ln );
2531
+ return fetchDbmsOutputLine (connection, cb);
2532
+ }
2533
+ });
2534
+ }
2535
+ ` ` `
2536
+
2537
+ Another way is to wrap the ` DBMS_OUTPUT .GET_LINE ()` call into a
2538
+ pipelined function and fetch the output using a SQL query. See
2539
+ [dbmsoutputpipe.js](https://github.com/oracle/node-oracledb/tree/master/examples/dbmsoutputpipe.js) for the full example.
2540
+
2541
+ The pipelined function could be created like:
2542
+
2543
+ ` ` ` sql
2544
+ CREATE OR REPLACE TYPE dorow AS TABLE OF VARCHAR2 (32767 );
2545
+ /
2546
+
2547
+ CREATE OR REPLACE FUNCTION mydofetch RETURN dorow PIPELINED IS
2548
+ line VARCHAR2 (32767 );
2549
+ status INTEGER ;
2550
+ BEGIN LOOP
2551
+ DBMS_OUTPUT .GET_LINE (line, status);
2552
+ EXIT WHEN status = 1 ;
2553
+ PIPE ROW (line);
2554
+ END LOOP ;
2555
+ END ;
2556
+ /
2557
+ ` ` `
2558
+
2559
+ To get DBMS_OUTPUT that has been created, simply execute the query
2560
+ using the same connection:
2561
+
2562
+ ` ` ` sql
2563
+ connection .execute (
2564
+ " SELECT * FROM TABLE(mydofetch())" ,
2565
+ [],
2566
+ { resultSet: true },
2567
+ function (err , result ) {
2568
+ . . .
2569
+ ` ` `
2570
+
2571
+ The query rows can be handled using a
2572
+ [ResultSet](http://localhost:8899/doc/api.md#resultsethandling).
2573
+
2574
+ Remember to first enable output using ` DBMS_OUTPUT .ENABLE (NULL )` .
2575
+
2497
2576
## <a name="lobhandling"></a> 11. Working with CLOB and BLOB Data
2498
2577
2499
2578
The [Lob Class](#lobclass) in node-oracledb implements the
0 commit comments