【问题标题】:ORA-00900: invalid SQL statement - when run a query using node-oracledbORA-00900: 无效的 SQL 语句 - 使用 node-oracledb 运行查询时
【发布时间】:2021-04-01 11:10:07
【问题描述】:

我以用户 AN 登录并在 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;

并且执行了它,这工作正常。之后,我使用 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!');
                    

但是当我设置 EXECUTE 查询时:

**connection**.execute(
      `execute **viewSystemUsers**;`,
       (err, result) => {
            if(err){
                  console.error(err);
                  return;
            }
           console.log(result);

我收到 [Error: ORA-00900: invalid SQL statement] { errorNum: 900, offset: 0 }。 谁能帮我解决它?非常感谢。

【问题讨论】:

  • node-oracledb 是否支持dbms_sql.return_result?如果没有,那么您可能需要在过程中使用OUT 参数(这是实现相同结果的典型方法)。
  • @MT0 你的意思是使用 sys_refcursor 变量作为输出参数,调用 proc 打印该变量?
  • 使用SYS_REFCURSOR变量作为OUT参数,调用过程,然后循环遍历变量得到结果集。
  • @M10,node-oracledb 是否支持 dbms_ouput.putline()?如果没有,如何从循环中获取数据?
  • 类似这样的东西:blogs.oracle.com/opal/…

标签: oracle plsql node-oracledb


【解决方案1】:
  • 您正在将字符串 execute ... 传递给数据库。但execute 不是 SQL 关键字 - 它是 SQL*Plus 命令。所以数据库不理解它并给出错误。而是使用begin viewSystemUsers() end;

  • 为了便于编程使用异步/等待,而不是回调

  • 查看 node-oracledb 示例 impres.js,因为这显示了隐式结果的使用(dbms_sql.return_result() 就是这样)。

  • 查看 node-oracledb 示例 plsqlproc.js

你的代码可能是这样的:

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();

【讨论】:

    猜你喜欢
    • 2010-12-29
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    相关资源
    最近更新 更多