简体   繁体   中英

node-oracledb bind variable of null

Using the node-oracledb package with node.js, I'm trying to delete a row in my oracle database that has a null value in it. I need to be able to pass a null value in as a bind variable but it is throwing an error:

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

The error returned is

{ Error: ORA-00908: missing NULL keyword errorNum: 908, offset: 46 }

You don't need a bind variable for that...

Given the following table:

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;

This should work:

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

what about change the query

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

it change null column and value to another character change (__) with your own character

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM