下面示例演示了如何在包声明中定义常量,以及在包体的子程序中定义本地变量。前者作用范围是整个包,对调用者可见;后者作用范围是当前子程序内部,对调用者不可见。

    示例:在子程序里声明变量和常量并赋值

    1. delimiter /
    2. CREATE OR REPLACE PACKAGE pkg_ware_mgmt as
    3. MAX_WARES constant number(2,0) := 10;
    4. MAX_DISTS_PER_WARE constant number(2,0) := 10;
    5. FUNCTION f_check_ware_exists(p_name IN varchar2) RETURN BOOLEAN ;
    6. END;
    7. /
    8. delimiter ;
    9. delimiter /
    10. CREATE OR REPLACE PACKAGE BODY pkg_ware_mgmt as
    11. FUNCTION f_check_ware_exists( p_name IN varchar2 ) RETURN BOOLEAN
    12. IS
    13. i_cnt number := -1;
    14. b_ret boolean := FALSE;
    15. BEGIN
    16. SELECT count(*) INTO i_cnt FROM ware WHERE w_name = p_name AND rownum < 2;
    17. b_ret := (i_cnt = 1);
    18. RETURN b_ret;
    19. EXCEPTION
    20. WHEN OTHERS THEN
    21. RETURN FALSE;
    22. END;
    23. END;
    24. /
    25. obclient> delimiter ;
    26. obclient>
    27. 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;
    28. +------+------+
    29. | COL1 | COL2 |
    30. +------+------+
    31. | 0 | 0 |
    32. +------+------+
    33. 1 row in set (0.05 sec)