我有一个程序需要SELECT
语句作为参数。
如果我从程序中运行SELECT语句,它运行正常,但是当我在程序中运行它时,我得到错误:
程序:
data_dump(query_in => 'select * from reservation where type not in ('H','PURGE','E') ',
file_in => 'export_'||months_from||''||months_to||'.csv',
directory_in => 'C:\Users\Administrator\Desktop\test',
delimiter_in => '|' );
查询本身给了我预期的结果。
select * from reservation where type not in ('H','PURGE','E', '|| other ||')
The error is: encountered the symbol 'H'.
是否可以因为IN条件周围的单引号?
右括号错误:
(trunc(a.update_date) between (select add_months(TRUNC(SYSDATE), -(]'|| months_from ||q'[') ) from dual) and (select add_months(TRUNC(SYSDATE), -(]'|| months_to ||q'[') from dual))]'
答案 0 :(得分:1)
您在字符串中传递引用的参数。你需要逃避这些内部引用:
data_dump(query_in => 'select * from reservation where type not in (''H'',''PURGE'',''E'') ',
file_in => 'export_'||months_from||''||months_to||'.csv',
directory_in => 'C:\Users\Administrator\Desktop\test',
delimiter_in => '|' );
或使用alternative quoting mechanism:
data_dump(query_in => q'[select * from reservation where type not in ('H','PURGE','E') ]',
...
与您的其他'您需要在自己的转义引号中包含该值:
data_dump(query_in => 'select * from reservation where type not in (''H'',''PURGE'',''E'','''|| other ||''')',
...
或(现在可能不太可读):
data_dump(query_in => q'[select * from reservation where type not in ('H','PURGE','E',']' || other || q'[')]',
...
您可以使用dbms_output
调试此类事件,打印生成的参数,并查看它是否与您手动运行的匹配。
使用您的第三个部分代码,您可以在(可能是数字)变量之后放置单引号:
(trunc(a.update_date) between (select add_months(TRUNC(SYSDATE), -(]'|| months_from ||q'[') ) from dual) and (select add_months(TRUNC(SYSDATE), -(]'|| months_to ||q'[') from dual))]'
^ ^
正如消息所说,你在第二次add_month
电话中错过了一个右括号;所以它应该是(...
是您尚未展示的陈述的其余部分):
q'[ ... (trunc(a.update_date) between (select add_months(TRUNC(SYSDATE), -(]'
|| months_from ||q'[) ) from dual) and (select add_months(TRUNC(SYSDATE), -(]'
|| months_to ||q'[) ) from dual))]'
同样,这是您可以通过打印生成的语句并检查它或尝试手动运行它来进行的基本调试。
此外,您不需要内部select from dual
内容或所有括号;你可以这样做:
q'[ ... trunc(a.update_date) between add_months(TRUNC(SYSDATE), -]'
|| months_from ||q'[) and add_months(TRUNC(SYSDATE), -]'|| months_to ||q'[)]'