ITPub博客

首页 > 数据库 > Oracle > oracle存储过程、触发器

oracle存储过程、触发器

Oracle 作者:gorfjt 时间:2012-12-20 09:25:49 0 删除 编辑
存储过程:
创建语法:
create or replace procedure 存储过程名[(param1 in type,param2 out type)]

is
变量1 类型(值范围); --content varchar2(40000);

变量2 类型(值范围);
begin
--pl/sql语句
end 存储过程名;

举例:其中有一个无参游标和一个有参游标:
create or replace procedure XXX
is
   --标记
   flag int:=0;
   --游标
   cursor curYYY is
         select *
           from X x,Y y
           where x.id=y.id;
    --参数游标     
    cursor curZZZ(id varchar2)is
          select * from P p where p.id=id; 
 
    rec_curZZZ curZZZ%rowtype;  
    rec_curYYY curYYY%rowtype;
begin
    --判断游标是否被打开,如果已经打开,首先关闭
  if curYYY%isopen then
    --关闭游标
    close curYYY;
  end if;
  --打开游标
  open curYYY;
  --进行循环
  loop
    --打开游标里的一条记录,赋到游标变量里
    fetch curYYY
      into rec_curYYY;
    --判断游标里是否还有记录,如果没有记录,关闭游标,退出循环
    if curYYY%notfound then
      close curYYY;
      exit;
    else
      if curZZZ%isopen then
         --关闭游标
         close curZZZ;
      end if;
      --打开游标
      open curZZZ(rec_curYYY.id);
      loop
        fetch curZZZ
          into rec_curZZZ;
          if curZZZ%notfound then
             close curZZZ;
             exit;
          else
             --pl/sql语句
           end if;
      end loop;  
    end if;
    --提交事务
    commit;
    coun:=coun+1;
    --打印标记
    dbms_output.put_line(coun);
  end loop;
exception
  when others then
    dbms_output.put_line('系统出错的日志为:' || to_char(SQLERRM));
    rollback;
end XXX;


触发器(转):
oracle 触发器可分为DML触发器、instead of 触发器、系统触发器和DDL触发器。其中只有DML的update 才支持列级别的触发器。这里主要讲解DML触发器语法:
create [or replace] trigger 触发器名 触发时间 触发事件 [of column1,column2]

on 表名 [for each row]

pl/sql 语句

其中:

触发器名:触发器对象的名称。

触发时间:指明触发器何时执行,该值可取:

before---表示在数据库动作之前触发器执行;

after---表示在数据库动作之后出发器执行。

触发事件:指明哪些数据库动作会触发此触发器,该值可取:

insert:数据库插入会触发此触发器;

update:数据库修改会触发此触发器;

delete:数据库删除会触发此触发器。

of column1,column2:这项代表更新该列时触发(列级),只有DML的update才支持列级的触发器。

表 名:数据库触发器想要指向的表。

for each row:对表的每一行触发器执行一次(行级)。如果没有这一选项,则只对整个表执行一次(表级)。

举例:下面的触发器在更新表auths之前触发,目的是不允许在周末修改表:

create trigger auth_secure

before insert or update or delete //对整表更新前触发

on auths

begin

if(to_char(sysdate,'DY')='SUN'

RAISE_APPLICATION_ERROR(-20600,'不能在周末修改表auths');

end if;

end auth_secure;



<!-- 正文结束 -->

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22548290/viewspace-1116067/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2009-09-16