Oracle 绑定变量 to_date 参数错误

Oracle Bind Variable to_date parameter Error

我正在编写使用 Python 脚本执行 SQL (Oracle) 语句的代码,如下所示:

PYTHON:

with open(os.path.join(WORKING_PATH, "sql", "fetchCalendar.sql"), 'r') as 
    fetch_date = fetch.read()   
#(Some other code)
CD = timestamp_tostring(ACTUALDATE)
print("CD:", CD)
cnnSB.cursor.execute(fetch_date, CD)

fetchCalendar.sql

SELECT YEARNBR as yearnbr, QUARTERNBR as quarternbr, MONTHNBR as monthnbr, WEEKNBR as weeknbr
 FROM MFS_MFSCALENDAR
WHERE ACTUALDATE = TO_DATE(:CD, 'DD-MON-YY')

当我使用以下值尝试 运行 时,会输出:

CD: 30-AUG-18

Traceback (most recent call last): File "AHT_Init.py", line 919, in init_load(data, end_of_week, cur, 3) File "AHT_Init.py", line 644, in init_load populate_rollup(modelid, test_data, train_data, rollup_cmd) File "AHT_Init.py", line 684, in populate_rollup cnnSB.cursor.execute(fetch_date, CD) cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number

(强调'ORA-01036: illegal variable name/number')

当然,我尝试更改变量名也无济于事。我尝试使用 Oracle SQL Developer 运行 SQL 代码(在出现提示时传入 '01-JUN-18',包括单引号), 并得到这个错误:

ORA-01858: a non-numeric character was found where a numeric was expected 01858. 00000 - "a non-numeric character was found where a numeric was expected" *Cause: The input data to be converted using a date format model was incorrect. The input data did not contain a number where a number was required by the format model. *Action: Fix the input data or the date format model to make sure the elements match in number and type. Then retry the operation.

不过,当我用以下内容替换有问题的行时:

WHERE ACTUALDATE = TO_DATE('01-JUN-18', 'DD-MON-YY');

代码按预期运行。 谁能告诉我问题可能是什么,以及如何解决? 非常感谢!

编辑:我知道 fetchCalendar.sql 代码中缺少分号;这对于 Python 如何在此上下文中与 Oracle 交互是必需的。为了测试目的,我在 Oracle SQL Developer 上尝试使用分号时添加了一个分号,但在为 .sql 文件删除分号时应该不会导致问题。

找到答案: 总之,wrongtype错误。

我的代码将 datetime.date 传递给需要字符串的 SQL to_date 转换。