存储过程架构
Create or replace procedure 名字[(参数)]
as or is
begin
sql语句
[exception]
end proc名字
调用:execute或者用块来处理
函数架构
create or replace function 名字[(参数)]
return 返回类型
as or is
begin
end 名字;
参数:参数名称 in ||out ||in out 数据类型
实例
过程
create or replace procedure proc_demo(i_id number,o_sum out number,o_count out number)
as
Begin
select count(*),sum(age) into o_count,o_sum from t where id>i_id;
exception
when no_data_found then dbms_output.put_line('你所查找的数据不存在');
when others then dbms_output.put_line('发生其他错误');
end proc_demo;
过程的调用
declare
o_sum number;
o_count number;
begin
proc_demo(15,o_sum,o_count);
end;
函数
create or replace function get_sum(i_id number,o_sum out number)
return number;
as
begin
select sum(age) into o_sum from t where id>i_id;
return o_sum;
exception
when no_data_found then dbms_output.put_line('所需要的数据没有找到');
when others then dbms_output.put_line('发生其它错误');
end get_sum;