如何在SQL SELECT语句中使用包常量?

ble*_*tin 42 sql oracle packages

如何在Oracle中的简单SELECT查询语句中使用包变量?

就像是

SELECT * FROM MyTable WHERE TypeId = MyPackage.MY_TYPE
Run Code Online (Sandbox Code Playgroud)

是否可能或仅在使用PL/SQL时(在BEGIN/END中使用SELECT)?

Rob*_*ijk 59

你不能.

对于要在SQL语句中使用的公共包变量,您必须编写包装函数以将值公开给外部世界:

SQL> create package my_constants_pkg
  2  as
  3    max_number constant number(2) := 42;
  4  end my_constants_pkg;
  5  /

Package created.

SQL> with t as
  2  ( select 10 x from dual union all
  3    select 50 from dual
  4  )
  5  select x
  6    from t
  7   where x < my_constants_pkg.max_number
  8  /
 where x < my_constants_pkg.max_number
           *
ERROR at line 7:
ORA-06553: PLS-221: 'MAX_NUMBER' is not a procedure or is undefined
Run Code Online (Sandbox Code Playgroud)

创建包装函数:

SQL> create or replace package my_constants_pkg
  2  as
  3    function max_number return number;
  4  end my_constants_pkg;
  5  /

Package created.

SQL> create package body my_constants_pkg
  2  as
  3    cn_max_number constant number(2) := 42
  4    ;
  5    function max_number return number
  6    is
  7    begin
  8      return cn_max_number;
  9    end max_number
 10    ;
 11  end my_constants_pkg;
 12  /

Package body created.
Run Code Online (Sandbox Code Playgroud)

现在它的工作原理:

SQL> with t as
  2  ( select 10 x from dual union all
  3    select 50 from dual
  4  )
  5  select x
  6    from t
  7   where x < my_constants_pkg.max_number()
  8  /

         X
----------
        10

1 row selected.
Run Code Online (Sandbox Code Playgroud)

问候,
Rob.

  • 你应该标记tour函数`deterministic`,否则每次需要`max_number`时Oracle都会不必要地调用它 (6认同)

小智 13

有一种更通用的方式对我来说很好.您使用输入常量名称(即schema.package.constantname)创建一个函数,它返回常量值.您可以通过绑定res变量来使用立即执行PL/SQL块(参见示例).

功能如下:

CREATE OR REPLACE FUNCTION GETCONSTANTVALUE (i_constant IN VARCHAR2)  RETURN NUMBER deterministic AS

   res number; 
BEGIN

   execute immediate 'begin :res := '||i_constant||'; end;' using out res;     
   RETURN res;

END;
/
Run Code Online (Sandbox Code Playgroud)

然后,您可以在任何SQL中使用任何包的常量,例如

select GETCONSTANTVALUE('PKGGLOBALCONSTANTS.constantname') from dual;
Run Code Online (Sandbox Code Playgroud)

像这样你只需要1个函数,你可以利用现有的packages.constants.

  • 好主意,唯一的缺点是错误(例如由于拼写错误)直到运行时才会出现 - 即使你引用了一个不存在的常量,你的查询也会编译而没有错误. (5认同)
  • 多亏了 `deterministic` 子句,这是一个不错的解决方案。 (2认同)

Nat*_*lls 7

注意:我只在Oracle 11g中尝试过这个.

我有类似的需求,发现简单地声明一个函数(没有包)返回所需的值更容易.要将它们放在ddl中以便导入,请记住使用/字符分隔每个函数声明.例如:

CREATE OR REPLACE FUNCTION UNDEFINED_INT RETURN NUMBER AS BEGIN RETURN 2147483646; END;
/
CREATE OR REPLACE FUNCTION UNDEFINED_SHORT RETURN NUMBER AS BEGIN RETURN 32766; END;
/
CREATE OR REPLACE FUNCTION UNDEFINED_LONG RETURN NUMBER  AS BEGIN RETURN 223372036854775806; END;
/
CREATE OR REPLACE FUNCTION UNDEFINED_FLOAT RETURN FLOAT  AS BEGIN RETURN .4028233E38; END;
/
CREATE OR REPLACE FUNCTION UNDEFINED_DOUBLE RETURN BINARY_DOUBLE  AS BEGIN RETURN to_binary_double('1.7976931348623155E308'); END;
/
CREATE OR REPLACE FUNCTION UNDEFINED_STRING RETURN VARCHAR AS BEGIN RETURN '?'; END;
/
Run Code Online (Sandbox Code Playgroud)

这允许您引用该函数,就好像它是一个常量值(例如,您甚至不需要括号).

例如(注意显示精度的to_char方法已被保留):SQL>从dual中选择undefined_int;

UNDEFINED_INT
-------------
   2147483646
Run Code Online (Sandbox Code Playgroud)

SQL>从dual中选择undefined_string;

UNDEFINED_STRING
--------------------------------------------------------------------------------
?
Run Code Online (Sandbox Code Playgroud)

SQL>从dual中选择undefined_double;

UNDEFINED_DOUBLE
----------------
      1.798E+308
Run Code Online (Sandbox Code Playgroud)

SQL>从双重选择to_char(undefined_double,'9.999999999999999EEEE');

TO_CHAR(UNDEFINED_DOUBL
-----------------------
 1.797693134862316E+308
Run Code Online (Sandbox Code Playgroud)

SQL>从双重选择to_char(undefined_double,'9.99999999999999999EEEE');

TO_CHAR(UNDEFINED_DOUBLE,
-------------------------
 1.79769313486231550E+308
Run Code Online (Sandbox Code Playgroud)