oracle过程中的动态SQL和out字段
当我使用这个时,我收到错误:
PROCEDURE GET_BY_CRIT(vchFilter varchar2(500),
intCantTotal OUT INT,
curResult OUT sys_refcursor)
IS
BEGIN
OPEN curResult FOR
'SELECT COLUMN1,COLUMN2 FROM SOME_TABLE WHERE '||vchFilter
EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM SOME_TABLE WHERE '||vchFilter
INTO intCantTotal
END
错误:
ORA-00936: missed expression
但是当我单独执行每个句子时,它会正确运行。
I get error when I use this:
PROCEDURE GET_BY_CRIT(vchFilter varchar2(500),
intCantTotal OUT INT,
curResult OUT sys_refcursor)
IS
BEGIN
OPEN curResult FOR
'SELECT COLUMN1,COLUMN2 FROM SOME_TABLE WHERE '||vchFilter
EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM SOME_TABLE WHERE '||vchFilter
INTO intCantTotal
END
Error:
ORA-00936: missed expression
But when I execute each sentence by separate it run correcly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到的错误似乎没有意义。 Oracle 应该抛出编译错误,因为函数的参数没有长度。
vchFilter
应声明为VARCHAR2
,而不是VARCHAR2(500)
。此外,正如 Lolo 在评论中指出的那样,PL/SQL 块中的语句需要以分号终止。
另请注意,不能保证第二个 SQL 语句将看到与第一个 SQL 语句相同的 COUNT,除非您可以保证
SOME_TABLE
不会同时被任何其他会话修改。正在查询它。我通常对运行查询和执行单独计数的需要相当警惕——这通常表明一个更基本的问题。如果您需要 COUNT 与您正在运行的查询保持一致,您需要向查询添加一个分析 COUNT 并让调用者获取该列。The error you're getting doesn't seem to make sense. Oracle should be throwing a compilation error because parameters to functions don't have a length.
vchFilter
should be declared as aVARCHAR2
, not aVARCHAR2(500)
.Additionally, as Lolo pointed out in the comments, statements in a PL/SQL block need to be terminated with semicolons.
Be aware as well that there is no guarantee that second SQL statement will see the same COUNT that the first SQL statement did unless you can guarantee that
SOME_TABLE
is not being modified by any other sessions at the same time you're querying it. I'd generally be rather wary of a need to run a query and execute a separate count-- that generally indicates a more basic problem. If you need the COUNT to be consistent with the query you're running, you'd want to add an analytic COUNT to your query and let the caller fetch that column.