程序包
程序包的定义:
程序包是对相关过程、函数、变量、游标和异常等对象的封装 程序包由规范和主体两部分组成
优点:
模块化、更轻松的应用程序设计、信息隐藏、新增功能、性能更佳。
创建包头包体的基本操作如下:
create or replace package pack1 --创建包头/规范
is
aa number := 1 ; --在包头声明的为公有变量
procedure update_student (update_row in student %rowtype ); --声明一个更新过程
procedure insert_student (insert_row in student %rowtype ); --声明一个插入过程
end pack1 ; --结束包头
--Package created
create or replace package body pack1 --创建包体/主体
is
bb number := 2 ; --在包体声明的变量类私有变量
procedure insert_student (insert_row in student %rowtype ) --创建过程主体
as
begin
insert into student( id, name, age) values (insert_row.id ,insert_row.name ,insert_row.age );
dbms_output.put_line ('bb = ' ||bb ||'aa = ' || aa );
end insert_student; --结束过程主体
procedure update_student( update_row in student% rowtype) --创建过程主体
as
begin
update student s set s.name = '赵北' where s.id = update_row.id ;
end update_student ;--结束过程主体
end pack1 ;--结束主体/包体
--Warning: Package body created with compilation errors
SQL > show error; --查询错误
Errors for PACKAGE BODY HR.PACK1 :
LINE/ COL ERROR
----------------------------------------------------------------------------
5 /1 PLS -00103 : 出现符号 "BEGIN"在需要下列之一时: ; is with authid as
cluster order using external deterministic parallel_enable
pipelined result_cache 符号 ";" 被替换为 "BEGIN" 后继续。
10 /3 PLS -00103 : 出现符号 "PROCEDURE"
11 /5 PLS -00103 : 出现符号 "BEGIN"在需要下列之一时: ; is with authid as
cluster order using external deterministic parallel_enable
pipelined result_cache 符号 ";" 被替换为 "BEGIN" 后继续。
SQL >
SQL > ed --修改上次执行的代码块
SQL > /--执行修改的代码块
--Package body created
SQL > set serverout on; --打开输出开关
SQL > execute dbms_output.put_line (pack1.aa ); --包中的公共变量被输出
1
PL /SQL procedure successfully completed
SQL > execute dbms_output.put_line (pack1.bb ); --包中的私有变量不被输出
begin dbms_output.put_line (pack1.bb ); end;
--ORA-06550: 第 1 行, 第 34 列:
--PLS-00302: 必须声明 'BB' 组件
--ORA-06550: 第 1 行, 第 7 列:
--PL/SQL: Statement ignored
declare
row_student student %rowtype ; --声明行级变量
begin
row_student.id := 5;
row_student.name := '张飞';
row_student.age := 60;
pack1.insert_student (row_student );--调用包中的过程
end;
/
bb = 2aa = 1
PL /SQL procedure successfully completed
SQL > select * from student ;
ID NAME AGE
----------- -------------------- -----------
1 张三 20
2 李四 25
3 王五 30
4 麻子 30
5 张飞 60
SQL >
declare
row_student student %rowtype ; --声明行级变量
begin
row_student.id := 5;
row_student.name := '关羽';
row_student.age := 60;
pack1.update_student (row_student );--调用包中的过程
end ;
/
PL /SQL procedure successfully completed
SQL > select * from student ;
ID NAME AGE
----------- -------------------- -----------
1 张三 20
2 李四 25
3 王五 30
4 麻子 30
5 赵北 60
| ? |
程序包中的游标:
q游标的定义分为游标规范和游标主体两部分 q在包规范中声明游标规范时必须使用 RETURN 子句指定游标的返回类型 qRETURN子句指定的数据类型可以是: q用 %ROWTYPE 属性引用表定义的记录类型 q程序员定义的记录类型,例如 TYPE EMPRECTYP IS RECORD(emp_id INTEGER,salaryREAL)来定义的。 q不可以是number, varchar2, %TYPE等类型。
-----------------------------在程序包中创建显示游标---------------
create or replace package pack2 --创建包头
is
cursor student_cursor return student %rowtype ; --声明显示游标,但是不能跟is select子句
procedure student_pro ; --声明过程
end pack2 ;
create or replace package body pack2 --创建包体
is
cursor student_cursor return student %rowtype is select * from student ; --指定游标所关联的select
procedure student_p
|