下面示例演示了如何在包声明中定义常量,以及在包体的子程序中定义本地变量。前者作用范围是整个包,对调用者可见;后者作用范围是当前子程序内部,对调用者不可见。
示例:在子程序里声明变量和常量并赋值
delimiter /
CREATE OR REPLACE PACKAGE pkg_ware_mgmt as
MAX_WARES constant number(2,0) := 10;
MAX_DISTS_PER_WARE constant number(2,0) := 10;
FUNCTION f_check_ware_exists(p_name IN varchar2) RETURN BOOLEAN ;
END;
/
delimiter ;
delimiter /
CREATE OR REPLACE PACKAGE BODY pkg_ware_mgmt as
FUNCTION f_check_ware_exists( p_name IN varchar2 ) RETURN BOOLEAN
IS
i_cnt number := -1;
b_ret boolean := FALSE;
BEGIN
SELECT count(*) INTO i_cnt FROM ware WHERE w_name = p_name AND rownum < 2;
b_ret := (i_cnt = 1);
RETURN b_ret;
EXCEPTION
WHEN OTHERS THEN
RETURN FALSE;
END;
END;
/
obclient> delimiter ;
obclient>
obclient> SELECT pkg_ware_mgmt.f_check_ware_exists('W_NAME_1') col1 ,pkg_ware_mgmt.f_check_ware_exists('W_NAME_3') col2 FROM DUAL;
+------+------+
| COL1 | COL2 |
+------+------+
| 0 | 0 |
+------+------+
1 row in set (0.05 sec)