《Oracle PL/SQL开发指南》学习笔记32——源码调试——包(第一部分,包架构)

 

1. 前向引用(Forward Referencing)

"The execution block knows everything in its declaration block or external declaration  block(s). The forward-referencing stub lets the PL/SQL single-pass parser put the second  procedure declaration in its list of identifiers. It is added before the parser reads the first  procedure because single-pass parsers read from the top down. When the parser reads the  first procedure, it knows about the second procedure. The parser then validates the call  to the second procedure and looks for the implementation of second later in the program  to compile the code successfully. The parser raises a PLS-00328 error if the subprogram is  missing after reading the complete source code. 

NOTE  Java uses a two-pass parser and lets you avoid forward  declarations."

SQL> /* Formatted on 2018/12/9 22:03:46 (QP5 v5.256.13226.35538) */
SQL> DECLARE
  2     -- Placeholder for a forward-referencing stub.
  3     PROCEDURE FIRST (pv_caller VARCHAR2)
  4     IS
  5     BEGIN
  6        DBMS_OUTPUT.put_line ('"First" called by [' || pv_caller || ']');
  7        second ('First');
  8     END;
  9
 10     PROCEDURE second (pv_caller VARCHAR2)
 11     IS
 12     BEGIN
 13        DBMS_OUTPUT.put_line ('"Second" called by [' || pv_caller || ']');
 14     END;
 15  BEGIN
 16     FIRST ('Main');
 17  END;
 18  /
      second ('First');
      *
第 7 行出现错误:
ORA-06550: 第 7 行, 第 7 列:
PLS-00313: 在此作用域中没有声明 'SECOND'
ORA-06550: 第 7 行, 第 7 列:
PL/SQL: Statement ignored


SQL> ed
已写入 file afiedt.buf

  1  DECLARE
  2     -- Placeholder for a forward-referencing stub.
  3     PROCEDURE second(pv_caller VARCHAR2);
  4     PROCEDURE FIRST (pv_caller VARCHAR2)
  5     IS
  6     BEGIN
  7        DBMS_OUTPUT.put_line ('"First" called by [' || pv_caller || ']');
  8        second ('First');
  9     END;
 10     PROCEDURE second (pv_caller VARCHAR2)
 11     IS
 12     BEGIN
 13        DBMS_OUTPUT.put_line ('"Second" called by [' || pv_caller || ']');
 14     END;
 15  BEGIN
 16     FIRST ('Main');
 17* END;
 18  /
"First" called by [Main]
"Second" called by [First]

PL/SQL 过程已成功完成。

2. 重载(overloading)

/* Formatted on 2018/12/9 22:42:36 (QP5 v5.256.13226.35538) */
CREATE OR REPLACE PACKAGE not_overloading
IS
   FUNCTION adding (a NUMBER, b NUMBER)
      RETURN NUMBER;

   FUNCTION adding (one NUMBER, two NUMBER)
      RETURN BINARY_INTEGER;
END;


CREATE OR REPLACE PACKAGE overloading
IS
   FUNCTION adding (a NUMBER, b NUMBER)
      RETURN NUMBER;

   FUNCTION adding (a VARCHAR2, b NUMBER)
      RETURN NUMBER;

   FUNCTION adding (a NUMBER, b VARCHAR2)
      RETURN NUMBER;

   FUNCTION adding (a VARCHAR2, b VARCHAR2)
      RETURN BINARY_INTEGER;
END overloading;
SQL> ed
已写入 file afiedt.buf

  1  create or replace procedure test(abc number) is
  2  begin
  3  dbms_output.put_line(abc);
  4* end test;
SQL> /

过程已创建。

SQL> desc test
PROCEDURE test
参数名称                       类型                    输入/输出默认值?
------------------------------ ----------------------- ------ --------
 ABC                            NUMBER                  IN

SQL> ed
已写入 file afiedt.buf

  1  create or replace procedure test(cde number, abc number) is
  2  begin
  3  dbms_output.put_line(abc);
  4* end test;
SQL> /

过程已创建。

SQL> desc test
PROCEDURE test
参数名称                       类型                    输入/输出默认值?
------------------------------ ----------------------- ------ --------
 CDE                            NUMBER                  IN
 ABC                            NUMBER                  IN

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值