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

使用cx_Oracle绑定日期值触发ORA-00936错误,求解决方案

cx_Oracle绑定日期变量报错ORA-00936的解决思路

你的代码存在两个关键错误引发了ORA-00936错误:

  • SQL语句里错误使用了date :cu_perf_beg,Oracle绑定变量不需要加date前缀,这种写法会让数据库解析时判定缺少合法表达式。
  • 传参时给日期值套了单引号'2022-09-01',这会把值变成带引号的字符串,破坏了绑定变量的语法逻辑。

修正方案

方案1:直接传入日期字符串(依赖Oracle默认日期格式)

dat_ptd_sql = """
select 
    univ_prop_id,
    chain_id
from BA4DBOP1.zs_ptd_stack
where chain_ord = 1
    and sale_valtn_dt >= :cu_perf_beg
"""
cudb_cur.execute(dat_ptd_sql, cu_perf_beg = "2022-09-01")

方案2:传入Python datetime对象(更推荐,避免格式兼容问题)

from datetime import date

dat_ptd_sql = """
select 
    univ_prop_id,
    chain_id
from BA4DBOP1.zs_ptd_stack
where chain_ord = 1
    and sale_valtn_dt >= :cu_perf_beg
"""
cudb_cur.execute(dat_ptd_sql, cu_perf_beg = date(2022, 9, 1))

方案3:如果数据库日期格式非默认,使用TO_DATE显式转换

dat_ptd_sql = """
select 
    univ_prop_id,
    chain_id
from BA4DBOP1.zs_ptd_stack
where chain_ord = 1
    and sale_valtn_dt >= TO_DATE(:cu_perf_beg, 'YYYY-MM-DD')
"""
cudb_cur.execute(dat_ptd_sql, cu_perf_beg = "2022-09-01")

内容的提问来源于stack exchange,提问作者Asim Raja

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 01:50:14