Node.js Oracledb executeMany() 更新数据库失败

Node.js Oracledb executeMany() fail to update database

我正在使用 NodeJs 的 oracledb 包通过使用 executeMany() 在数据库中执行多个更新,不幸的是每次我尝试更新时我都会得到以下 错误:ORA-01722:无效数字.

来自数据库的数据类型: Data Types from Database

数据库中存在示例数据 Sample Data

输出错误: Error

Oracle数据库版本为11g

我哪里做错了??请帮忙

这是我的示例代码片段。

oracledb.getConnection(connAttrs_, function (err, connection) {
    if (err) {
        //Error connecting to DB
        res.set('Content-Type', 'application/json');
        res.status(500).send(JSON.stringify({
            status: 500,
            message: "Error connecting to DB",
            detailed_message: err.message
        }));
        console.log(err.message);
        return;
    }

    var sql = "UPDATE tblTestBulkUpdate SET NAME =:2 WHERE TO_NUMBER(ID) =:1";
    
    var binds = [
        [10, "updated"],
        [11, "updated two"],
        [20, "why this"],
        [22, "dummy data"],
        [30, "ok ok"],
        [40, "fig zero"],
        [45, "fig five"],
        [47, "fig seven"],

    ];

    var options = {
        autoCommit: true,  
        batchErrors: true, 
        bindDefs: [         
        
            { type: oracledb.NUMBER},
            { type: oracledb.STRING, maxSize: 50} 
            
        ]
    };
    
    connection.executeMany(sql, binds, options, function (err, result) {
        if (err)
          console.error(err);
        else {
          console.log("Result is:", result);
        }
      });
});

使用如下语法:

var binds = [
        [10, "updated"],
...

是 'bind by position',而不是 'bind by number',因此 10 映射到语句中解析的第一个绑定变量,该变量恰好称为“:2”。看到 doc 上面写着

The position of the array values corresponds to the position of the SQL bind parameters as they occur in the statement, regardless of their names. This is still true even if the bind parameters are named like :0, :1, etc.

您可以尝试使用这样的代码片段,因为 binds 变量首先包含字符串:

sql = `select * from dual where 'abc' = :2 and 123 = :1`;
binds = ['abc', 123];
result = await connection.execute(sql, binds);

如果您无法更改数据顺序,请尝试使用按名称语法绑定。

em_insert1.js 中有一个按名称绑定语法的示例:

const sql = "INSERT INTO no_em_tab values (:a, :b)";

const binds = [
  { a: 1, b: "Test 1 (One)" },
  { a: 2, b: "Test 2 (Two)" },
  { a: 3, b: "Test 3 (Three)" },
  { a: 4 },
  { a: 5, b: "Test 5 (Five)" }
];

// bindDefs is optional for IN binds but it is generally recommended.
// Without it the data must be scanned to find sizes and types.
const options = {
  autoCommit: true,
  bindDefs: {
    a: { type: oracledb.NUMBER },
    b: { type: oracledb.STRING, maxSize: 15 }
  }
};