您好,欢迎访问一九零五行业门户网

Oracle Procedure 存储过程语法

子程序和程序包总结:子程序是命名的pl/sql块,可带参数并可在需要时随时调用。pl/sql有两种类型的子程序,即过程和函数。过程用
子程序和程序包
总结:
子程序是命名的pl/sql块,可带参数并可在需要时随时调用。
pl/sql有两种类型的子程序,即过程和函数。
过程用于执行特定的任务,函数用于执行任务并返回值。
程序包是对相关类型、变量、常量、游标、异常、过程和函数的封装。
程序包由包规范和包主体两部分组成。
包规范是包的接口,包含公用对象及其类型。
包主体实现包规范中的游标和子程序,包主体中的声明仅限于在包内使用。
程序包中游标的定义分为游标规范和游标主体两部分。
语法及示例:
1、存储过程
创建存储过程的语法:
create [or replace] procedure procedure_name
[(parameter_list)]
{is|as}
[local_declarations]
begin
executable_statements
[exception
exception_handlers]
end [procedure_name];
其中:procedure_name是过程的名称。
parameter_list是参数列表。
local_declarations是局部声明。
executable_statements是可执行语句。
exception_handlers是异常处理程序。
示例1:演示创建过程(参数列表中为in参数赋予一个默认值,不能为out、in out参数赋予默认值)
create or replace procedure find_emp(emp_no in number:=7900)
as
empname varchar2(20);
begin
select ename into empname from emp where empno=emp_no;
dbms_output.put_line('雇员姓名是 '||empname);
exception
when no_data_found then
dbms_output.put_line('雇员编号未找到');
end find_emp;
调用过程:execute procudure_name(parameters_list);
也可以在过程里面调用,直接写上procudure_name而不必写execute。
示例2:演示创建带out参数的过程
create or replace procedure test(value1 varchar2,value2 out number)
is
identity number;
begin
select sal into identity from emp where empno=value1;
if identityvalue2:=1000;
else
value2:=500;
end if;
end;
调用带out参数的过程:
declare
value2 number;
begin
test('7900',value2);
dbms_output.put_line(value2);
end;
示例3:演示创建带in out参数的过程
create or replace procedure swap(p1 in out number,p2 in out number)
is
v_temp number;
begin
v_temp:=p1;
p1:=p2;
p2:=v_temp;
end;
调用带in out参数的过程:
declare
num1 number:=100;
num2 number:=200;
begin
swap(num1,num2);
dbms_output.put_line('num1= '||num1);
dbms_output.put_line('num2= '||num2);
end;
示例4:将过程的执行权限授予其他用户
grant execute on find_emp to scott;
grant execute on swap to public;
将find_emp过程的执行权限授予给用户scott,将执行swap过程的权限授予所有数据库用户。
删除过程语法:drop procedure procudure_name;
2、函数
定义函数的语法如下:
create [or replace] function function_name
[(parameter_list)]
return datatype
{is|as}
[local_declarations]
begin
executable_statements
[exception
exception_handlers]
end [function_name];
其中:function_name是函数的名称。
parameter_list是参数列表。
local_declarations是局部声明。
executable_statements是可执行语句。
exception_handlers是异常处理程序。
使用函数时注意:形式参数必须只使用数据库类型,不得使用pl/sql类型。函数的返回类型也必须是数据库类型。
函数不能单独执行,只能通过sql语句或pl/sql程序块来调用。
示例5:演示如何创建函数
create or replace function fun_hello
return varchar2 is
begin
return '朋友,您好';
end;
调用函数:select fun_hello from dual;
函数的授权:同过和的授权一样具体请看示例4。
删除函数:drop function function_name
过程和函数的差异
过程 函数
作为pl/sql语句执行 作为表达式的一部分调用
在规范中不包含return子句 必须在规范中包含return子句
不返回任何值 必须返回单个值
可以包含return语句,但是与函数不同,它不能用于返回值 必须包含至少一条return语句
3、程序包
创建包规范的语法:
create [or replace] package package_name
is|as
[public type and item declarations]
[subprogram specifications]
end [package_name];
其中:package_name是包的名称。
public type and item declarations是声明类型、常量、变量、异常和游标等。
subprogram specifications声明pl/sql子程序。
示例6:演示创建程序包规范
create or replace package pack_op is
procedure pro_print_ename(id number);
procedure pro_print_sal(id number);
function fun_re_date(id number) return date;
end;
创建包主体的语法:
create [or replace] package body package_name
is|as
[public type and item declarations]
[subprogram bodies]
[begin
initialization_statements]
end [package_name];
其中:package_name是包的名称。
public type and item declarations是声明类型、常量、变量、异常和游标等。
subprogram bodies是定义公共和私有pl/sql子程序。
示例7:演示创建程序包主体
create or replace package body pack_op is
procedure pro_print_ename(id number) is
name emp.ename%type;
begin
select ename into name from emp where empno=id;
dbms_output.put_line('职员姓名:'||name);
end pro_print_ename;
procedure pro_print_sal(id number) is
salary emp.sal%type;
begin
select sal into salary from emp where empno=id;
dbms_output.put_line('职员工资:'||salary);
end pro_print_sal;
function fun_re_date(id number) return date is
bedate emp.hiredate%type;
begin
select hiredate into bedate from emp where empno=id;
return bedate;
end fun_re_date;
end pack_op;
示例8:调用程序包中创建的过程和函数
exec pack_op.pro_print_ename(7900);
exec pack_op.pro_print_sal(7900);
select pack_op.fun_re_date(7900) from dual;
示例9:演示程序包中的游标
创建包规范
create or replace package pack_emp is
cursor cur_emp return emp%rowtype;
procedure pro_cur;
end pack_emp;
其它类似信息

推荐信息