You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Knex.raw执行Oracle插入SQL报ORA-00933错误问题排查

问题根因

触发ORA-00933的直接原因是拼接的SQL语句末尾多了分号;。
Oracle的Node.js驱动(Knex连接Oracle底层依赖oracledb驱动)执行单条SQL时,会将传入的字符串完整作为SQL语法主体发送给数据库解析。分号是SQL Developer、sqlplus这类客户端工具用来分隔多条语句的标记,不属于SQL本身的语法内容,驱动不会自动截断末尾分号,数据库解析到分号位置时就会判定SQL未正常结束,抛出错误。
SQL Developer这类工具执行语句前会自动过滤末尾的分号,所以复制完全相同的语句在工具中可以正常运行。报错信息中标记的offset=407,刚好对应SQL字符串末尾分号的位置,完全匹配该问题特征。

即时修复方案

直接删除custom_query字符串末尾的分号即可,修改后的SQL拼接代码如下:

const custom_query = `insert into ${this.tableName} (job_id, counts, type, content_ids, search_query, republishing_reason, duration, creation_date, last_update_date, created_by, last_updated_by) values ('${job_id}','${counts_string}', '${payload.type}', '${content_ids_string}', '${search_query_string}', '${republishing_reason}', ${payload.duration}, to_date('${creation_date}','YYYY-MM-DD HH24:MI:SS'), to_date('${last_modified_date}','YYYY-MM-DD HH24:MI:SS'), ${created_by}, ${last_updated_by})`
后续排查&优化建议
  • 禁止使用字符串拼接方式生成SQL,存在严重SQL注入风险,同时无法自动处理字段值中的单引号转义、类型匹配问题。如果字段值中包含单引号,会直接打破SQL的引号闭合,触发同类语法错误。建议改用Knex原生的插入语法与参数绑定,自动处理上述问题,参考写法:
const insertRecord = {
  job_id: uuid.v4(),
  counts: counts_string,
  type: payload.type,
  content_ids: content_ids_string,
  search_query: search_query_string,
  republishing_reason: republishing_reason,
  duration: payload.duration,
  creation_date: this.knex().raw(`to_date(?, 'YYYY-MM-DD HH24:MI:SS')`, [creation_date]),
  last_update_date: this.knex().raw(`to_date(?, 'YYYY-MM-DD HH24:MI:SS')`, [last_modified_date]),
  created_by: created_by,
  last_updated_by: last_updated_by
}
await this.knex(this.tableName).insert(insertRecord)
  • 如果去掉分号后仍报错,优先检查传入的字符串类型字段值中是否存在未转义的单引号,这类问题同样会触发SQL语法解析错误。
  • 检查Knex连接Oracle的配置项,确认没有开启自动追加语句分隔符的冗余配置。

内容的提问来源于stack exchange,提问作者Quan Khuc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 02:18:20