包中添加新过程应在尾部添加

在看高级应用开发手册时,在对象依赖部分,注意到这个细节。
其他对象依赖包中的过程或函数,如果这个过程或函数在包中的位置发生了变化,那么依赖当前过程或函数的对象会被置为INVALID状态。

SQL> CREATE OR REPLACE PACKAGE P_TEST AS
  2     PROCEDURE P1;
  3     FUNCTION F1 RETURN NUMBER;
  4  END;
  5  /
Package created.
SQL> CREATE OR REPLACE FUNCTION F2 RETURN NUMBER AS
  2  BEGIN
  3     RETURN P_TEST.F1;
  4  END;
  5  /
FUNCTION created.
SQL> SELECT OBJECT_NAME, STATUS      
  2  FROM USER_OBJECTS
  3  WHERE OBJECT_NAME IN ('P_TEST', 'F2');
OBJECT_NAME                    STATUS
------------------------------ -------
F2                             VALID
P_TEST                         VALID
SQL> CREATE OR REPLACE PACKAGE P_TEST AS
  2     PROCEDURE P1;
  3     FUNCTION F1 RETURN NUMBER;
  4     PROCEDURE P3;
  5  END;
  6  /
Package created.
SQL> SELECT OBJECT_NAME, STATUS
  2  FROM USER_OBJECTS
  3  WHERE OBJECT_NAME IN ('P_TEST', 'F2');
OBJECT_NAME                    STATUS
------------------------------ -------
F2                             VALID
P_TEST                         VALID
SQL> CREATE OR REPLACE PACKAGE P_TEST AS
  2     PROCEDURE P1;
  3     PROCEDURE P2;
  4     FUNCTION F1 RETURN NUMBER;
  5     PROCEDURE P3;
  6  END;
  7  /
Package created.
SQL> SELECT OBJECT_NAME, STATUS
  2  FROM USER_OBJECTS
  3  WHERE OBJECT_NAME IN ('P_TEST', 'F2');
OBJECT_NAME                    STATUS
------------------------------ -------
F2                             INVALID
P_TEST                         VALID

如果将新增过程置于包的尾部,则不会影响依赖关系。而如果将新增过程置于某些已有过程或函数的前面,则所有依赖这些过程或函数的对象将被置于INVALID状态,这是由于依赖关系和过程或函数在包中的位置有关。
不过这种位置只是过程或函数的排序,和具体所在行数无关,而且只和过程或函数有关,如果新增变量则不会产生影响:

SQL> ALTER FUNCTION F2 COMPILE;
FUNCTION altered.
SQL> SELECT OBJECT_NAME, STATUS
  2  FROM USER_OBJECTS
  3  WHERE OBJECT_NAME IN ('P_TEST', 'F2');
OBJECT_NAME                    STATUS
------------------------------ -------
F2                             VALID
P_TEST                         VALID
SQL> CREATE OR REPLACE PACKAGE P_TEST AS
  2     G_NUM NUMBER;
  3     PROCEDURE P1;
  4     PROCEDURE P2;
  5     FUNCTION F1 RETURN NUMBER;
  6     PROCEDURE P3;
  7  END;
  8  /
Package created.
SQL> SELECT OBJECT_NAME, STATUS
  2  FROM USER_OBJECTS
  3  WHERE OBJECT_NAME IN ('P_TEST', 'F2');
OBJECT_NAME                    STATUS
------------------------------ -------
F2                             VALID
P_TEST                         VALID
This entry was posted in ORACLE and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *