1 create or replace package testpak
2 is
3 procedure mysub(a int);
4 function myfunc(b int) return int;
5* end testpak;
SQL> /
Package created.
SQL> remark 包体:是一个独立于包头的数据字典对象。包体只能在包头完成编译后进行编译。包体中带有包头中描述的前向子程序代码段;
SQL> remark 注:包体是可选的;包头和包体中的过程和函数必须一致,包括子程序名和其参数名,以及参数模式。
SQL> create or replace package body testpak
2 as
3 procedure mysub(a int)
4 is
5 begin
6 dbms_output.put_line(a*a);
7 end mysub;
8
9 function myfunc(b int) return int
10 is
11 begin
12 return b+1;
13 end myfunc;
14 end testpak;
15 /
SQL> remark 创建包体
SQL> create or replace package body testpak
2 is
3 function add(m int) return int
4 is
5 begin
6 thecount:=thecount+m;
7 return thecount;
8 end add;
9 procedure getcount
10 is
11 begin
12 dbms_output.put_line(thecount);
13 end getcount;
14 end;
15 /
SQL> remark 包中子过程的重载
SQL> remark 在包的内部,过程和函数可以被重载(overloding) 也就是说:可以有一个以上名称相同的,但参数不同的过程或函数。
SQL> remark 示例:子过程中的重载
SQL> create or replace package testpak
2 as
3 thecount int :=0;
4 function add(m int) return int;
5 function add(m int , n int) return int;
6 procedure getcount;
7 end;
8 /
Package created.
SQL> remark 创建包体:实现包头中的函数和过程
SQL> create or replace package body testpak
2 is
3 function add(m int) return int
4 is
5 begin
6 thecount:=thecount+m;
7 return thecount;
8 end add;
9
10 function add(m int , n int) return int
11 is
12 begin
13 thecount:=thecount+m+n;
14 return thecount;
15 end add;
16
17 procedure getcount
18 is
19 begin
20 dbms_output.put_line(thecount);
21 end getcount;
22 end;
23 /
Package body created.
SQL> remark 调用函数和存储过程
SQL> declare
2 result int;
3 begin
4 dbms_output.put_line('调用add(m int)的函数:');
5 result :=testpak.add(5);
6 dbms_output.put_line('当前包内thecount的值为:'||testpak.thecount);
7 dbms_output.put_line('调用add(m int,n int)的函数:');
8 result :=testpak.add(5,5);
9 dbms_output.put_line('当前包内thecount的值为:'||testpak.thecount);
10 end;
11 /
调用add(m int)的函数:
当前包内thecount的值为:5
调用add(m int,n int)的函数:
当前包内thecount的值为:15