定义:存储过程(Stored Procedure )是一组为了完成特定功能的SQL 语句集,经编译后存储在数据库中。用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。
存储过程的优点:
1.存储过程只在创造时进行编译,以后每次执行存储过程都不需再重新编译,而一般SQL语句每执行一次就编译一次,所以使用存储过程可提高数据库执行速度。
2.当对数据库进行复杂操作时(如对多个表进行Update、Insert、Query、Delete时),可将此复杂操作用存储过程封装起来与数据库提供的事务处理结合一起使用。
3.存储过程可以重复使用,可减少数据库开发人员的工作量。
4.安全性高,可设定只有某用户才具有对指定存储过程的使用权。
存储过程常用到的一些写法:
create or replace procedure 名称(记得必须是大写)(
v_frcode varchar2, --参数1
out_cur out sys_refcursor --参数2游标 用于返回多个值
)is
t_count int:=0; --int类型
t_pbid varchar(100); --字符串类型
t_degree varchar(100); --字符串类型
t_money number; --金额
t_date date; --日期类型
begin
//===========================for 循环操作=======================
for comdata in(
select ziduan1,zuduan2,ziduan3,ziduan4 from table1 where 条件
) loop
//可以操作每一条数据 ,可以直接获取到每一个字段 comdata.ziduan1
//可在for 循环中操作
if comdata.ziduan1=1 then
t_count:=t_count+1;
else
end if;
end loop;
//===============返回游标,参数里面定义游标,用于页面上展现数据==============
open out_cur for
select * from table1 where 1=1;
//==============用游标当作中间临时表来进行其他操作=====================
cursor cusname(名称) is
select b.orgid,b.orgdegree from base_org_info b where b.status='0'
open cusname;
loop
fetch cusnmae into t_pbid,t_degree;
exit when cusname%notfound;
update base_org_info set orgdegree='1' where
end loop;
//=================输出某些数据 或者拼接sql 的时候,输出出来,方便查看结果==============
DBMS_OUTPUT.PUT_LINE('总条数:'||t_total);
DBMS_OUTPUT.PUT_LINE('新增pb00_01的条数:'||t_inserttotal);
DBMS_OUTPUT.PUT_LINE('更新pb00_01的条数:'||t_updatetotal);
DBMS_OUTPUT.PUT_LINE('更新pb02_01的条数:'||t_updatetotal2);
//====================返回给服务器端,用于页面接收后抛出异常==============
if t_dbbcbxbc_LJ = 0
then
Raise_application_error(-20020, '该患者未补偿大病补充保险,不允许结算!!!');
end if;
//=====================================
end;