1、存储过程
存储过程可以看做是oracle的语句集,调用存储过程执行一系列的数据操作,也可以通过传入参数,经过一系列运算之后得到输出参数。
1、1存储过程创建
无参存储过程
create or replace procedure pr_test as
v_variable1 number(2);--定义变量
begin
v_variable1:=1;--变量赋值
dbms_output.put_line(v_variable1);--dbms输出
exception when others then
return ;--异常处理
end;
带参数存储过程
create or replace procedure pr_test(i_variable in number,
o_variable out number) as
v_variable1 number(2); --定义变量
begin
v_variable1 := i_variable + 1; --变量赋值
o_variable := v_variable1; --将
dbms_output.put_line(o_variable); --dbms输出
exception
when others then
return; --异常处理
end;
2、函数
函数与存储过程类似,同样都是执行已系列的操作,区别在于
1、创建方式不同,存储过程使用procedure声明,函数使用function声明
2、存储过程可以有无返回参数,但函数至少需要有一个返回值
3、存储过程的执行,需要execute,或者begin end包裹,而函数不需要,
可以直接使用select function() 方式进行直接调用
2、1函数创建
返回一个参数
create or replace function fn_test(i_variable in number) return number as
v_variable1 number(2); --定义变量
begin
v_variable1 := i_variable + 1; --变量赋值
return v_variable1;
exception
when others then
return 0; --异常处理
end;
返回游标
create or replace function fn_test(i_variable in number) return sys_refcursor as
v_variable1 number(2); --定义变量
type_cur sys_refcursor; --定义一个游标
begin
v_variable1:=i_variable+1;
open type_cur for
select i_variable,v_variable1,3 from dual;
return type_cur;
end;
还有一种,函数返回表类型的返回值,这种返回类型我基本没有用过,所以没有使用代码,有兴趣的朋友可以去搜索看看。