并非所有变量都绑定Node js和Oracle db

时间:2019-05-07 07:50:04

标签: javascript node.js oracle plsql

嗨,我有以下使用oracle db的PL / SQL。当我使用它。

        var = 'DECLARE
       IN_SCHOOL_NUM NUMBER;
       OUT_RESULTS CLOB;
       OUT_ERROR VARCHAR2(100);
       BEGIN 
       IN_SCHOOL_NUM := 3321;
       PKG.School(
       IN_SCHOOL_NUM => IN_SCHOOL_NUM,
       OUT_RESULTS => OUT_RESULTS,
       OUT_ERROR => OUT_ERROR);
       DBMS_OUTPUT.PUT_LINE(OUT_RESULTS);END'

schoolDetails(req, callback){
    oracledb.getConnection({
      user: 'xxxx',
      password:  'XXXX',
      connectString: 'xxxxxxxxxxxx'
      },
      function(err, conn){
         if(err) {console.error(err); return;}
         conn.execute(sql, 
            {OUT_RESULTS: {DIR: oracledb.BIND_OUT, type: oracledb.OBJEC}},
         function(err, result){
           if(!err){
              console.log(result.OUT_RESULTS);
              return callback(null, result)
               } else 
                console.log("error"+ err);
                return callback(err);
            }
        )}
     )}

运行此命令时出现错误ORA-01008:并非所有变量都已绑定。

请让我知道如何解决此问题。谢谢

2 个答案:

答案 0 :(得分:0)

下面的代码不是您可以理解的功能:

DECLARE
       IN_SCHOOL_NUM NUMBER;
       OUT_RESULTS CLOB;
       OUT_ERROR VARCHAR2(100);
BEGIN 
       IN_SCHOOL_NUM := 3321;
       PKG.School(
       OUT_RESULTS => OUT_RESULTS,
       OUT_ERROR => OUT_ERROR);
       DBMS_OUTPUT.PUT_LINE(OUT_RESULTS);
END;

因此没有什么可绑定的。但是我的第一个问题是您是否尝试在SQLDeveloper或SQLPlus中运行它?如果运行良好,那就很好。尽管IN_SCHOOL_NUM完全不在任何地方使用。

因此,假设PKG是一个程序包而不是一个架构,我认为您想将过程School作为来自Node的远程过程来调用。

您还可以检查School是否确实没有输入参数。也许通话应该更像:

PKG.School(
   IN_SCHOOL_NUM => 3321,
   OUT_RESULTS => OUT_RESULTS,
   OUT_ERROR => OUT_ERROR);

因为这种情况下,将例程更改为以下内容可能会得到一些有意义的结果:

        var = ' 
       PKG.School(
        IN_SCHOOL_NUM => 3321,
        OUT_RESULTS => OUT_RESULTS,
        OUT_ERROR => OUT_ERROR);'

schoolDetails(req, callback){
    oracledb.getConnection({
      user: 'xxxx',
      password:  'XXXX',
      connectString: 'xxxxxxxxxxxx'
      },
      function(err, conn){
         if(err) {console.error(err); return;}
         conn.execute(sql, 
            {OUT_RESULTS: {DIR: oracledb.BIND_OUT, type: oracledb.OBJECT}},
         function(err, result){
           if(!err){
              console.log(result.OUT_RESULTS);
              return callback(null, result)
               } else 
                console.log("error"+ err);
                return callback(err);
            }
        )}
     )}

答案 1 :(得分:0)

尝试类似的东西:

'use strict';

const oracledb = require('oracledb');
const config = require('./dbconfig.js');

const myproc = `
  create or replace procedure school (
    in_school_num in number,
    out_results out clob,
    out_error out varchar2) as
  begin
    out_error := '';  -- all OUT variables should be initialized or set
    out_results := 'I done very good in class this year at school ' || IN_SCHOOL_NUM;
  end;`;

const plsql = `call school(:in_school_num_bv,:out_results_bv,:out_error_bv)`;

async function run() {
  let connection;

  try {
    connection = await oracledb.getConnection(config);

    await connection.execute(myproc);

    const binds = {
      in_school_num_bv: 123,
      out_results_bv: { type: oracledb.STRING, dir: oracledb.BIND_OUT },
      out_error_bv: { type: oracledb.VARCHAR2, dir: oracledb.BIND_OUT }
    };

    const result = await connection.execute(plsql, binds);

    console.log(result.outBinds.out_results_bv);
    //console.log(result.outBinds.out_error_bv);
  } catch (err) {
    console.error(err);
  } finally {
    if (connection) {
      try {
        await connection.close();
      } catch (err) {
        console.error(err);
      }
    }
  }
}

run();

这使用Node 8的async / await样式,通常比摆弄回调要容易。