node-oracledb绑定null变量

时间:2018-04-19 20:15:26

标签: javascript node.js oracle oracledb oracledb-npm

node-oracledb包与node.js一起使用,我试图删除我的oracle数据库中具有 null 值的行。我需要能够将 null 值作为绑定变量传递,但它会抛出错误:

var query = "delete from table where event IS :event";
var bind_vars = [null];
connection.execute(query, bind_vars, { autoCommit: true }, function(error, results) {});

返回的错误是

  

{错误:ORA-00908:缺少NULL关键字errorNum:908,偏移量:46}

2 个答案:

答案 0 :(得分:0)

你不需要一个绑定变量...

鉴于下表:

create table t (
  c number
);

insert into t values (1);
insert into t values (null);
insert into t values (3);
insert into t values (null);
insert into t values (5);

commit;

这应该有效:

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

async function runTest() {
  let conn;

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

    let result;
    let value = 1;

    if (value === null) {
      result = await conn.execute('delete from t where c is null');
    } else { // other values should have a bind
      result = await conn.execute('delete from t where c = :value', [value]);
    }

    // Note that the work was not committed.
    console.log('Rows deleted:', result.rowsAffected);
  } catch (err) {
    console.error(err);
  } finally {
    if (conn) {
      try {
        await conn.close();
      } catch (err) {
        console.error(err);
      }
    }
  }
}

runTest();

答案 1 :(得分:0)

有关更改查询的内容

var query = "delete from table where nvl(event, '__') = nvl(:event, '__')";
var bind_vars = [null];
connection.execute(query, bind_vars, { autoCommit: true }, function(error, results) {});

它将空列和值更改为另一个字符 用自己的角色更改(__)