cx_Oracle CREATE TABLE AS returns ORA-01036: illegal variable name/number

时光毁灭记忆、已成空白 提交于 2019-12-11 05:17:18

问题


I'm trying to CREATE TABLE AS using cx_Oracle on Python 2.4.6

The following code:

    query = '''CREATE TABLE TMP_STATS_1 AS
    select NUM from INF_CARD where IMPORT_DATE between
    to_date(:datepass || ' 00:00:00','dd/mm/yyyy hh24:mi:ss') and
    to_date(:datepass || ' 23:59:59','dd/mm/yyyy hh24:mi:ss')'''
    curs.execute(query, datepass=datep)

Returns: cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number

Taking out the CREATE TABLE TMP_STATS_1 AS and leaving only the SELECT statement, works perfectly. Also, when using CREATE TABLE AS statement on SQLPLUS it runs correctly.

Is there any specific syntax for CREATE TABLE AS to be used in cx_Oracle? Tried to find examples for this, but none found so far.


回答1:


You are better off creating a procedure in Oracle that can create your table, and then call the procedure form Python:

create or replace procedure my_pro(p_table_name in varchar2, p_date_pass in date) as
 q1 varchar2(4000);
begin

 q1 := 'CREATE TABLE '|| p_table_name ||' AS
 select NUM from INF_CARD where IMPORT_DATE between
 trunc('||p_date_pass||') and trunc('||p_date_pass||') + 1 - (1/24/60/60)';

 EXECUTE IMMEDIATE q1;
end;

and then call the procedure from Python:

import cx_Oracle
con = cx_Oracle.connect('###YourPath###')
cur = con.cursor()
cur.callproc('my_pro', ('TMP_STATS_1', datep))
cur.close()
con.close()


来源:https://stackoverflow.com/questions/23433467/cx-oracle-create-table-as-returns-ora-01036-illegal-variable-name-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!