Chinaunix首页 | 论坛 | 博客
  • 博客访问: 475359
  • 博文数量: 111
  • 博客积分: 3146
  • 博客等级: 中校
  • 技术积分: 939
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-07 11:23
个人简介

Nathing

文章分类

全部博文(111)

文章存档

2016年(2)

2015年(1)

2014年(31)

2012年(2)

2011年(9)

2010年(36)

2009年(30)

我的朋友

分类: Oracle

2014-01-15 17:18:04

今天写了一个触发器,报ORA-04088错误.
在网上查阅是说:

for each row的, 执行过程中, 不能 SELECT / UPDATE / DELETE 当前的被触发的表.
感觉很经典的一句话.

网上有其他方案:感觉不太好用.
------解决方案--------------------
不能对基表进行操作。
用自治事务试下,在declare里面加入
PRAGMA AUTONOMOUS_TRANSACTION; 
------解决方案--------------------

贴一下我的触发器,这里面就出现了当前被触发的表,所以有问题:

点击(此处)折叠或打开

  1. create or replace trigger trig_t_felix_resource
  2.     after insert or delete or update on t_felix_resource for each row--每更新一行就会触发一次

  3. begin
  4.   declare

  5.   cursor deptments is
  6.     select * from PF_ORG_DEPARTMENT t where t.name like '%电业局' or t.code ='SGSBB';

  7.   cursor types is
  8.     select * from T_NAME_MAPPING m where m.type = '物理资源类' and m.code != '链路';

  9.   v_deptName VARCHAR2(20);--地市的名称
  10.   v_typeName VARCHAR2(20);
  11.   v_devNum number;

  12.   begin
  13.     --获取地市的名称
  14.     for v_dept in deptments loop--for循环时v_dept不需要申明
  15.       v_deptName := substr(v_dept.name,0,2);
  16.       if (v_deptName = 'xx') then
  17.         v_deptName := 'yyy';
  18.       end if;
  19.       --获取资源的六大类型
  20.       for v_type in types loop
  21.         v_typeName := v_type.code;

  22.         select count(t.id) into v_devNum
  23.         from t_resourceinfo t, t_felix_resource t1 
  24.         where t.org_code in (select t.code
  25.                               from pf_org_department t
  26.                              start with t.id = v_dept.id
  27.                             connect by prior id = parent_id)
  28.          and t.bus_res_type_id in
  29.              (select ty.id
  30.                 from t_resource_type ty
  31.                start with ty.id = v_type.name
  32.               connect by prior id = pid)
  33.          and t.id = t1.resource_id;
  34.          --删除对应的一条记录
  35.          delete from t_trig_dev_station ttds
  36.          where ttds.location=v_deptName and ttds.devtype=v_typeName;
  37.          --插入对应的一条新记录
  38.          insert into t_trig_dev_station (id, location, devtype, devnum)
  39.          values (sys_guid(), v_deptName, v_typeName, v_devNum);
  40.       end loop;
  41.     end loop;
  42.   end;
  43. end;

阅读(4697) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~