错误:ORA-00907:oracle 缺少右括号

Posted

技术标签:

【中文标题】错误:ORA-00907:oracle 缺少右括号【英文标题】:Error: ORA-00907: missing right parenthesis with oracle 【发布时间】:2020-06-22 17:04:16 【问题描述】:

执行以下查询时出现此错误,但检查括号已完成。

错误:[错误:ORA-00907:缺少右括号]

 import oracledb from 'oracledb'
 const  ORACLE_USER, ORACLE_PASSWORD  = ORACLE_CONFIG

connection = await oracledb.getConnection(
  user: ORACLE_USER,
  password: ORACLE_PASSWORD,
  connectString: 'xxxx'
)
console.log('------------------*************[Info] Successfully connected to Oracle!************-----------------')

const  customerId, flag, Name, insertDate  = params
const result = await connection.execute(`Insert into table1
    (customer_id, name, status_flag, insert_date, update_date)
     Values
    ($customerId, $Name, $flag, TO_DATE($insertDate, 'MM/DD/YYYY HH24:MI:SS'), TO_DATE($insertDate, 'MM/DD/YYYY HH24:MI:SS'))`)

【问题讨论】:

分享完整代码 @dassum 我认为问题出在查询中 请勿将字符串文字用于数据值,因为存在安全风险和性能影响。使用答案中所示的绑定变量。 【参考方案1】:

Alessandro 是对的 - 您正在使用模板字符串,它只是用您的字符串替换模板文字。因此,对于一些示例数据,您在运行时的插入语句实际上如下所示:

Insert into table1
(customer_id, name, status_flag, insert_date, update_date)
 Values
(111, Joe User, Y, TO_DATE(06/22/2020 16:07:00, 'MM/DD/YYYY HH24:MI:SS'), TO_DATE(06/22/2020 16:07:00, 'MM/DD/YYYY HH24:MI:SS'))

这不是有效的 SQL。您可以像 Alessandro 建议的那样在模板字符串周围加上单引号,这样可以解决问题,但请注意您的代码容易受到 SQL 注入的攻击。

node-oracledb docs have some examples of using bind variables,这是 SQL 的最佳实践。例如,以下是使用“按位置绑定”语法的方法:

const result = await connection.execute(`Insert into table1
    (customer_id, name, status_flag, insert_date, update_date)
     Values
    (:customerId, :name, :flag, TO_DATE(:insertDate, 'MM/DD/YYYY HH24:MI:SS'), TO_DATE(:insertDate, 'MM/DD/YYYY HH24:MI:SS'))`, 
  [ customerId, flag, Name, insertDate, insertDate ])

【讨论】:

【参考方案2】:

您的代码中有很多没有撇号的字符串。假设 $Name$flag$insertDate 是字符串,您需要在它们之前和之后放置 ',就像在 MM/DD/YYYY HH24:MI:SS 之前和之后一样。

【讨论】:

这些是字符串文字,将在运行时替换为值 我知道,但是有些语言没有为 oracle 传递变量的撇号,我认为 Java 就是其中之一,因为我在 Java 中使用了一个软件,并且在我传递变量时没有明确的撇号。你试过了吗?

以上是关于错误:ORA-00907:oracle 缺少右括号的主要内容,如果未能解决你的问题,请参考以下文章

Python 操作Redis

python爬虫入门----- 阿里巴巴供应商爬虫

Python词典设置默认值小技巧

《python学习手册(第4版)》pdf

Django settings.py 的media路径设置

Python中的赋值,浅拷贝和深拷贝的区别