在Oracle SQL开发人员中声明绑定变量

发布于 2025-02-12 16:34:54 字数 1416 浏览 0 评论 0原文

我想尝试通过Oracle SQL开发人员执行此查询,但是我总是收到错误消息,该消息是

ORA-01722: invalid number
01722. 00000 -  "invalid number"
*Cause:    The specified number was invalid.
*Action:   Specify a valid number.
DEFINE po_header_ids VARCHAR(1000) := '1,2,3';

    SELECT 
        PHA.SEGMENT1
    FROM 
        PO.PO_HEADERS_ALL       PHA
    WHERE     1=1
        AND (
            :po_header_ids = 0 OR 
            :po_header_ids IS NULL OR
            PHA.PO_HEADER_ID IN (
                SELECT regexp_substr(:po_header_ids,'[^,]+',1,level)
                FROM dual CONNECT BY
                regexp_substr(:po_header_ids ,'[^,]+',1,level) IS NOT NULL
            )
        )

在Oracle SQL Deverloper中输入的参数

有人可以告诉我哪个部分是错误的?

谢谢。

这是我成功执行的查询,以及po_header_id

po_header_id数据类型

DEFINE po_header_ids varchar(1000);
SELECT regexp_substr(:po_header_ids,'[^,]+',1,level) 
FROM dual CONNECT BY
regexp_substr(:po_header_ids,'[^,]+',1,level) IS NOT NULL

上述查询的输出

I'd like to try to execute this query via Oracle SQL Developer, but I always got the error message which is

ORA-01722: invalid number
01722. 00000 -  "invalid number"
*Cause:    The specified number was invalid.
*Action:   Specify a valid number.
DEFINE po_header_ids VARCHAR(1000) := '1,2,3';

    SELECT 
        PHA.SEGMENT1
    FROM 
        PO.PO_HEADERS_ALL       PHA
    WHERE     1=1
        AND (
            :po_header_ids = 0 OR 
            :po_header_ids IS NULL OR
            PHA.PO_HEADER_ID IN (
                SELECT regexp_substr(:po_header_ids,'[^,]+',1,level)
                FROM dual CONNECT BY
                regexp_substr(:po_header_ids ,'[^,]+',1,level) IS NOT NULL
            )
        )

parameters which are entered in the oracle sql deverloper

Can someone tell me which part is wrong?

Thank you.

Here's the query that I executed successfully and the data type of the PO_HEADER_ID

PO_HEADER_ID Data Type

DEFINE po_header_ids varchar(1000);
SELECT regexp_substr(:po_header_ids,'[^,]+',1,level) 
FROM dual CONNECT BY
regexp_substr(:po_header_ids,'[^,]+',1,level) IS NOT NULL

Output of the above query

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

蓝色星空 2025-02-19 16:34:54
  • 定义声明一个替代变量,该变量将在代码中的&前缀,并在客户端应用程序(IE IE SQL Developer)中处理,就好像发现了一个find-Replace在声明。
  • 变量声明一个绑定变量,该变量将在代码中以的前缀为前缀,并由服务器上的SQL引擎处理。

您还需要更改::po_header_ids = 0 to :po_header_ids ='0',因为绑定变量是字符串而不是数字。

因此,您想要:

VARIABLE po_header_ids VARCHAR2(1000);

BEGIN
  :po_header_id := '1,2,3';
END;
/

SELECT PHA.SEGMENT1
FROM   PO.PO_HEADERS_ALL       PHA
WHERE  :po_header_ids = '0'
OR     :po_header_ids IS NULL
OR     PHA.PO_HEADER_ID IN (
         SELECT TO_NUMBER(regexp_substr(:po_header_ids,'[^,]+',1,level))
         FROM   dual
         CONNECT BY
                regexp_substr(:po_header_ids ,'[^,]+',1,level) IS NOT NULL
       );

db<

  • DEFINE declares a substituition variable which will be prefixed by & in the code and is processed in the client application (i.e. in SQL Developer) as if a find-replace occurs on the statement.
  • VARIABLE declares a bind-variable which will be prefixed by : in the code and is processed by the SQL engine on the server.

You also need to change: :po_header_ids = 0 to :po_header_ids = '0' as the bind variable is a string and not a number.

So you want:

VARIABLE po_header_ids VARCHAR2(1000);

BEGIN
  :po_header_id := '1,2,3';
END;
/

SELECT PHA.SEGMENT1
FROM   PO.PO_HEADERS_ALL       PHA
WHERE  :po_header_ids = '0'
OR     :po_header_ids IS NULL
OR     PHA.PO_HEADER_ID IN (
         SELECT TO_NUMBER(regexp_substr(:po_header_ids,'[^,]+',1,level))
         FROM   dual
         CONNECT BY
                regexp_substr(:po_header_ids ,'[^,]+',1,level) IS NOT NULL
       );

db<>fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文