Oracle PL/SQL 包的使用

本文详细介绍了PL/SQL包的概念及其组成部分,包括包的说明部分和包体,并通过实例展示了如何创建包、包体以及如何在包中实现过程和函数的重载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SQL> set serveroutput on;
SQL> remark 包:是由存储在一起的相关对象组成的PL SQL结构。包有两个独立的部分:即说明部分和包体,这两个部分是独立的存在数据字典中的;
SQL> remark 包的说明:包的说明包含了有关包的相关信息。该部分中不包含包的代码部分;

SQL> edit;
Wrote file afiedt.buf

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 /

Package body created.

SQL> remark .............................................................
SQL> remark 包和作用域:包头中声明的任何对象都是在其作用域中,使用包名.对象名 可以调用包中对应的对象;
SQL> remark 在包体中:包头中的 对象可以直接的引用,可以不用包名.
SQL> remark 包头中的变量是全局的 子过程中的变量是局部的;
SQL> create or replace package testpak
2 as
3 thecount int:=0;
4 function add(m int) return int;
5 procedure getcount;
6 end;
7 /

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 procedure getcount
10 is
11 begin
12 dbms_output.put_line(thecount);
13 end getcount;
14 end;
15 /

Package body created.

SQL> remark 调用包体中的函数;
SQL> declare
2 result int;
3 begin
4 result:=testpak.add(5);
5 dbms_output.put_line('函数的运算结果为:'||result);
6 dbms_output.put_line('当前包内的参数值为:'||testpak.thecount);
7 testpak.getcount;
8 end;
9 /
函数的运算结果为:5
当前包内的参数值为:5
5

PL/SQL procedure successfully completed.

SQL> /
函数的运算结果为:10
当前包内的参数值为:10
10

PL/SQL procedure successfully completed.

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

PL/SQL procedure successfully completed.

SQL> spool off;
[size=x-large][/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值