清风的blog 优然探索

我的第一个ORACLE触发器

CREATE OR REPLACE TRIGGER "KPIPLANUPDATE"
BEFORE
UPDATE OF "ISDEL"
ON "TB_KPIPLAN"
REFERENCING OLD AS sOlder
NEW AS sNew FOR EACH ROW

declare
  PRAGMA AUTONOMOUS_TRANSACTION; --没有这句的话,不能同时更新一个表了
  type temp_cur is ref cursor;
  cur_trail temp_cur;
  ParentID  varchar(20);
  v_sql     varchar(4000);

begin
  v_sql := 'select KpiPlanID from tb_kpiplan  A Where A.ParentID = ' ||
           :sNew.kpiPlanID;
  --打开游标
  if not cur_trail%isopen then
    open cur_trail for v_sql;
  end if;

  --循环更新数据
  loop
    fetch cur_trail
      into ParentID;
    exit when(cur_trail%notfound);
    v_sql := 'update Tb_Kpiplan Set IsDel = ''Y'' Where KpiPlanID =' ||
             ParentID;
    --执行更新数据库操作
    execute immediate v_sql;
    commit;
  end loop;

  --关闭游标
  if cur_trail%isopen then
    close cur_trail;
  end if;

end;
 

2009年1月31日 | 发布:admin | 分类:学习收藏 | 评论:0

发表留言: