I logged in as user AN and created a procedure in sql developer:
CREATE OR REPLACE PROCEDURE **viewSystemUsers**
AS
sysRefCursor SYS_REFCURSOR;
BEGIN
OPEN sysRefCursor
FOR
SELECT USERNAME, USER_ID, PASSWORD FROM dba_users;
dbms_sql.return_result(sysRefCursor);
END;
And executed it, this worked normally. After that i successfully connected as the same user using node-oracledb:
oracle.getConnection(
{
user : "AN",
password: "AN123",
connectString: "localhost:1521/orcl"
},
(error, **connection**) => {
if(error){
console.error(error.message);
return;
}
else
console.log('connect sucessfully!');
But when i set the EXECUTE query:
**connection**.execute(
`execute **viewSystemUsers**;`,
(err, result) => {
if(err){
console.error(err);
return;
}
console.log(result);
I received [Error: ORA-00900: invalid SQL statement] { errorNum: 900, offset: 0 }. Can anyone help me to fix it? Thank you very much.
You are passing the string execute ...
to the database. But execute
is not a SQL keyword - it is a SQL*Plus command. So the database doesn't understand it and gives an error. Instead do begin viewSystemUsers() end;
.
For ease of programming use async/await, not callbacks
Review the node-oracledb example impres.js becuase this shows the use of Implicit Results (which is what dbms_sql.return_result()
is).
Review the node-oracledb example plsqlproc.js.
You code could be like:
const oracledb = require('oracledb');
const dbConfig = require('./dbconfig.js');
if (process.platform === 'darwin') {
oracledb.initOracleClient({libDir: process.env.HOME + '/Downloads/instantclient_19_8'});
}
let sql, binds, options, result;
sql = `begin viewSystemUsers(); end;`;
binds = [];
options = { outFormat: oracledb.OUT_FORMAT_OBJECT };
async function run() {
let connection;
try {
connection = await oracledb.getConnection(dbConfig);
result = await connection.execute(sql, binds, options);
console.dir(result, { depth: null });
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
run();