如何在 node-oracledb 5.1 中执行存储过程时出现编译错误

问题描述

我正在尝试执行一个原始存储过程,它在 sql Developer 中显示了一些编译错误。但它没有在 execute() 方法的回调函数中返回。

ex.runQuery  = async (data)=>{
  //{ "STATUS": "SUCCESS",data: { result },object_key_seq: data[i].object_key_seq,type: data[i].type,i: i }
  //{ "STATUS": "ERROR","ERROR": utils.parseError(err,query),i: i }
  let connection = null;
  let resutl = null;

  try{
    connection = await oraconnect.getPoolConnection()
    if(data.object_type == 'SEQUENCE' || data.object_type == 'TABLE' || data.object_type == 'VIEW' || data.object_type == 'INDEX'){
      data.script = data.script.slice(0,-1);
    }
    if(data.script.slice(-1) == '/'){
      data.script = data.script.slice(0,-1);
    }
    resutl = await oraconnect.query(connection,data.script,[],100);
  }catch(err){
    console.log(err);
    return { "STATUS": "ERROR","ERROR": {errorMessage:err.message},object_key_seq: data.object_key_seq,type: data.type,object_name :data.object_name}

  }finally{
    if (connection) {

      try {
        await   oraconnect.doRelease(connection);
        //await connection.close();
      } catch (err) {
        console.error(err);
      }
    }
  }

  return { "STATUS": "SUCCESS",data: { resutl },object_name: data.object_name}
}
@H_404_12@ @H_404_12@

以上是我用过的。我期待第一个 catch 块中出现错误,如下图所示

enter image description here

解决方法

您的问题没有显示在 node-oracledb 中执行的语句文本,但 SQL Dev 屏幕截图似乎显示您正在创建或编译存储过程。

在 node-oracledb 中创建 PL/SQL 过程和函数时,当前版本 5.1 不支持 PL/SQL 编译错误返回的“success with info”错误。这在增强请求 https://github.com/oracle/node-oracledb/issues/823 中注明。

您可以通过查询 user_errors 来手动检查问题,例如:

    await connection.execute(
      `create or replace procedure badproc() as
       begin
           INVALID
       end;`);
    const r = await connection.execute(
      `select line,position,text
       from user_errors
       where name = 'BADPROC' and type = 'PROCEDURE'
       order by name,type,line,position`,[],{ outFormat: oracledb.OUT_FORMAT_OBJECT }
    );
    if (r.rows.length) {
      console.error(r.rows[0].TEXT);
      console.error('at line ',r.rows[0].LINE,'position',r.rows[0].POSITION);
    }

输出如下:

PLS-00103: Encountered the symbol ")" when expecting one of the following:

   <an identifier> <a double-quoted delimited-identifier>
   current delete exists prior

at line  1 position 19

尝试调用这样一个无效的过程会产生预期的错误:

    const r2 = await connection.execute(`begin badproc(1); end;`);
    console.log(r2);

给出:

[Error: ORA-06550: line 1,column 7:
PLS-00905: object CJ.BADPROC is invalid
ORA-06550: line 1,column 7:
PL/SQL: Statement ignored] {
  errorNum: 6550,offset: 6
}