【发布时间】:2021-02-03 00:04:42
【问题描述】:
我编写了一个将查询作为输入的过程,当此查询在 Oracle 开发人员上独立运行时,我尝试用于测试的查询由单引号 (') 组成,它可以无缝运行。但是当以字符串格式传递相同的查询来执行过程时,我得到了无效的表达式错误。
为了纠正这个错误,我在使用单引号的地方添加了双引号(''),之后我得到了右括号丢失错误。我什至尝试使用 CHR(39),它是单引号的 ASCII 值,但这也没有缓解。
有什么方法可以传递包含此类特殊字符的查询。
查询供参考:
select c1 as Order,c2 as lineId,
listagg(concat(concat(concat(tkey,' : '),concat(name,' : ')), status), ': ')
WITHIN GROUP (ORDER BY c2) AS status from ABC
where
name in ('XY','LM','GT','GZ','AB')
AND
c2 in(select distinct(c2) from LMN
where
c1 in (select c1 from LMN
where
c1 > (select (cast ((systimestamp - numtodsinterval(1,'hour')) at time zone 'UTC' as date) - date '1970-01-01') * 86400 from dual)
AND
c1 < (select (cast (systimestamp at time zone 'UTC' as date) - date '1970-01-01') * 86400 from dual))
AND
from_system is not null) GROUP BY c1,c2
以上查询作为execute.proceudre_name('query')传递
PS 在使用 q-quote 机制时,查询如下:
execute getTransactionDetails(q'{select c1 as Order,c2 as lineId,
listagg(concat(concat(concat(tkey,' : '),concat(name,' : ')), status), ': ')
WITHIN GROUP (ORDER BY c2) AS status from ABC
where
name in ('XY','LM','GT','GZ','AB')
AND
c2 in(select distinct(c2) from LMN
where
c1 in (select c1 from LMN
where
c1 > (select (cast ((systimestamp - numtodsinterval(1,'hour')) at time zone 'UTC' as date) - date '1970-01-01') * 86400 from dual)
AND
c1 < (select (cast (systimestamp at time zone 'UTC' as date) - date '1970-01-01') * 86400 from dual))
AND
from_system is not null) GROUP BY c1,c2;}');
得到错误:
Error starting at line : 3 in command -
listagg(concat(concat(concat(tkey,' : '),concat(name,' : ')), status), ': ')
Error report -
Unknown Command
SP2-0044: For a list of known commands enter HELP
and to leave enter EXIT.
>>Query Run In:Query Result 1
【问题讨论】:
-
你不需要
SELECT ... from dual- 只需使用c1 > cast ((systimestamp - numtodsinterval(1,'hour')) at time zone 'UTC' as date) - date '1970-01-01') * 86400 -
感谢@WernfriedDomscheit 的建议!!
标签: string oracle stored-procedures