0

使用带有 node.js 的node-oracledb包,我试图删除我的 oracle 数据库中包含值的行。我需要能够将值作为绑定变量传递,但它会引发错误:

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 }

4

2 回答 2

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();
于 2018-04-19T20:45:24.703 回答
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) {});

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

于 2020-05-04T14:40:01.110 回答