Oracle SQL 命令未正确结束,而 运行 通过 Node.js 的子查询

Oracle SQL command not properly ended while running a subquery through Node.js

我正在尝试使用此子查询

获取 table 中存在的所有列

我正在使用这些参数调用我的代码

let idsquery="select COLUMN_Name from ids_columns where table_id = (select table_id from ids_tables where table_name ='ZR_INVOICE_DETAILS');";
idsFunction(idsquery,icallback);

这是我的代码

const oracledb = require('oracledb');
const idsObj=require('../config').idsObj;
let error;
let user;
function idsconnection(query,callback){
  // var query="select COLUMN_Name from ids_columns where table_id=2016";
  console.log(query);
  oracledb.getConnection(
      idsObj,
      function(err, connection) {
        if (err) {
          console.log('failed to connect',err);
          error = err;
          return;
        }
        connection.execute(query, [], function(err, result) {
          if (err) {
            console.log('failed to execute',err);
            error = err;
            return;
          }
          // console.log('column names are',result.metaData);
          // console.log('rows are',result.rows);
          // console.log('rows count is',result.rows.length);

          connection.close(function(err) {
            if (err) {
              console.log('failed to close connection',err);
            }
            // console.log('callback is ',callback);
            callback(result)
          });
        })
      }
  );
}

module.exports=idsconnection;

这段代码在我调用它时工作正常

let idsquery="select COLUMN_Name from ids_columns where table_id = 2012;";
idsFunction(idsquery,icallback);

像这样

但是当我执行第一个查询时出现此错误

failed to execute { [Error: ORA-00933: SQL command not properly ended] errorNum:933, offset: 125 }

查询本身看起来没问题。错误提到 "offset: 125" 指向右括号。

如果您重写该查询以便它使用连接(并避免子查询)会有帮助吗,例如

let idsquery="SELECT column_name FROM ids_columns c JOIN ids_tables t ON c.table_id = t.table_id WHERE t.table_name = 'ZR_INVOICE_DETAILS';";

正如@alex-poole 在评论中提到的,问题(或第一个问题)将是您在语句中有一个尾随分号:

let idsquery="select COLUMN_Name from ids_columns where table_id = (select table_id from ids_tables where table_name ='ZR_INVOICE_DETAILS');";

改为:

let idsquery="select COLUMN_Name from ids_columns where table_id = (select table_id from ids_tables where table_name ='ZR_INVOICE_DETAILS')";

Oracle SQL 不包含分号。令人困惑的是,PL/SQL 需要分号,SQL 等工具也使用分号,例如 SQL*Plus to say "this is the end of the statement, execute everything before here".

(潜在的)第二个问题是您没有使用绑定变量。你可能想做:

let query="select COLUMN_Name from ids_columns where table_id = (select table_id from ids_tables where table_name = :tn)";

connection.execute(query, ['ZR_INVOICE_DETAILS'], function(err, result) { . . . 

绑定变量提高了可伸缩性并有助于防止 SQL 注入安全问题。