子程序:模块化、可重用性、可维护性。包括过程和函数。
过程(存储过程)
create or replace procedure query_employee(a 过程参数 参数类型)is和as一样
过程参数 1、输入参数in 默认值 传常量 2、输出参数 out 变量 3、输入输出参数in out 传赋值变量 如
create or replace procedure query_employee(eno number, name out varchar2, salary out number)
is
begin
select ename,sal into name,salary from emp where empno = eno;
exception
when no_data_found then
raise_application_error(-20000,'该雇员不存在');
end;
调用:declare v_name varchar2(50); v_sal number(5);
begin query_employee(7369,v_name , v_sal ) ; end;
函数
与过程一样,但必须返回值
create or replace function get_result(num1 number,num2 in out number)
return number as
v_result number(6);
v_remainder number;
begin
v_result := num1 / num2;
v_remainder := mod(num1,num2);
num2 := v_remainder;
return v_result;
exception
when zero_divide then
raise_application_error(-20000,'不能除以0');
end;
可以在匿名块中 定义变量:=get_result(。。)
过程 | 函数 |
作为PL/SQL执行 | 作为表达式的一部分调用 |
规范中不包含return,写了也不能返回出来 | 必须包含return |
不返回任何值 | 必须返回单个值 |
外部调用使用 | 作为表达式可以返回复合类型 |
1、建立包规范:定义公用变量、公用过程
2、建立包体 :定义私用属性、私有过程或函数、公用过程或函数(包规范中有声明的过程或函数)
3、通过包名和包体中的子程序名调用,只能是公用的
允许子程序重载!
存储过程中只能使用动态游标sys_refcursor而包中可以在规范或包体开始声明故能自定义!
包的优点:1、模块化 2、轻松应用程序设计,一个表定义一个包,包含所有的子程序 3、信息隐藏 4、新增功能 5、性能佳
子程序和包的信息
select * from user_objects 所有表、名称和类型!
SQL*PLUS 登录 账号:scott 密码 tiger 主机字符串 数据库名 as sysdba