Javascript 通过Node.js运行子查询时,Oracle SQL命令未正确结束
我正在尝试使用此子查询获取表中的所有列 我用这些参数调用我的代码Javascript 通过Node.js运行子查询时,Oracle SQL命令未正确结束,javascript,sql,node.js,oracle,node-oracledb,Javascript,Sql,Node.js,Oracle,Node Oracledb,我正在尝试使用此子查询获取表中的所有列 我用这些参数调用我的代码 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('oracle
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 }
查询本身看起来不错。错误提到“偏移量:125”,它指向右括号 如果您重写该查询,使其使用联接(并避免子查询),会有帮助吗
正如@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*Plus等工具也使用分号表示“这是语句的结尾,在这里之前执行所有内容”
(潜在的)第二个问题是您没有使用绑定变量。您可能想做:
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注入安全问题。您确定您所说的代码确实有效吗?包括结尾的分号?我认为这两个版本都不应该存在,它会在JDBC、动态SQL等调用中导致错误,但不确定节点是否正确。您所说的代码实际上不会与SQL语句末尾的分号一起工作。删除分号(导致当前错误)后,需要将相等运算符更改为使用“in”或“exists”。我更喜欢“in”,但要小心使用空值和“in”。@AlexPoole非常感谢您的评论,我删除了分号,效果很好。@DanMcGhan我不需要在中使用,因为我知道我的查询肯定会返回唯一的值。@Pranaytaniru啊,对不起。如果是单行抓取,那么您就很好了。:)不,它不起作用,当我尝试你提到的查询时,它给出了相同的错误。a我删除了你查询中的分号,它起作用了。但多亏了你,我学会了如何使用连接重写查询。
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) { . . .