oracle过程中的动态SQL和out字段

发布于 2024-12-18 18:05:07 字数 486 浏览 2 评论 0原文

当我使用这个时,我收到错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瓜 2024-12-25 18:05:07

您收到的错误似乎没有意义。 Oracle 应该抛出编译错误,因为函数的参数没有长度。 vchFilter 应声明为 VARCHAR2,而不是 VARCHAR2(500)

此外,正如 Lolo 在评论中指出的那样,PL/SQL 块中的语句需要以分号终止。

PROCEDURE GET_BY_CRIT(vchFilter varchar2,
                      intCantTotal OUT integer,
                      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;

另请注意,不能保证第二个 SQL 语句将看到与第一个 SQL 语句相同的 COUNT,除非您可以保证 SOME_TABLE 不会同时被任何其他会话修改。正在查询它。我通常对运行查询和执行单独计数的需要相当警惕——这通常表明一个更基本的问题。如果您需要 COUNT 与您正在运行的查询保持一致,您需要向查询添加一个分析 COUNT 并让调用者获取该列。

PROCEDURE GET_BY_CRIT(vchFilter varchar2,
                      curResult OUT sys_refcursor)
IS
BEGIN
    OPEN curResult FOR
    'SELECT COLUMN1,COLUMN2, COUNT(*) OVER () cnt FROM SOME_TABLE WHERE '||vchFilter;
END;

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 a VARCHAR2, not a VARCHAR2(500).

Additionally, as Lolo pointed out in the comments, statements in a PL/SQL block need to be terminated with semicolons.

PROCEDURE GET_BY_CRIT(vchFilter varchar2,
                      intCantTotal OUT integer,
                      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;

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.

PROCEDURE GET_BY_CRIT(vchFilter varchar2,
                      curResult OUT sys_refcursor)
IS
BEGIN
    OPEN curResult FOR
    'SELECT COLUMN1,COLUMN2, COUNT(*) OVER () cnt FROM SOME_TABLE WHERE '||vchFilter;
END;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文