您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Oracle中变异表处理办法

2024/10/26 23:06:13发布26次查看
在oracle数据库中,变异表是当前被dml语句修改的表。而对于数据库上的triggrt(触发器)来说,变异表就是trigger在其上定义的表。需
oracle变异表
在oracle数据库中,变异表是当前被dml语句修改的表。
而对于数据库上的triggrt(触发器)来说,变异表就是trigger在其上定义的表。
需要明确的是trigger中sql语句不能进行如下操作:
1)读或者修改触发语句的任何变异表,包括触发表本身。
2)读或者修改触发表的约束表中的主关键字,唯一关键字和外部关键字。
而除此之外的其他列都可以修改。
如何突破变异表?
方法一是用两个触发器,一个行级的,一个语句级的(速度快,推荐!)
方法二是用dbms_job.run包(但运行的时间过长,2秒左右)
方法三是可以使用autocommit方法解决(但破坏了事务的一致性,多数情况下是错误的)。
下面是包 + 行级触发器 + 语句级触发器的实例:
在使用oracle行级触发器时,如果需要对本表进行dml操作(包括select),,一般都会报错:
ora-04091: 表 /**tablename**/ 发生了变化,触发器/函数不能读···
一般可以使用“ 包 + 行级触发器 + 语句级触发器 ”组合使用来解决。
// 创建包
create or replace package brpms.fxydata as
  type t_col1 is table of brpms.tb_risk_fxy.guid%type index by binary_integer;
  v_guid t_col1;
  v_row  binary_integer := 0;
end;
// 创建行级触发器
create or replace trigger brpms.t_iffxymodified
  before update of fxfxjdon brpms.tb_risk_fxy
  for each row
begin
  if :new.jhlx = '1' then
    if :new.fxfxjd = '已发布' then
      brpms.fxydata.v_row  := brpms.fxydata.v_row + 1;
      brpms.fxydata.v_guid(brpms.fxydata.v_row) := :new.guid;
    end if;
  end if;
end;
// 创建语句级触发器
create or replace trigger brpms.t_copy_y2r
  after update of fxfxjd on brpms.tb_risk_fxy
declare
  s_yjhid brpms.tb_risk_fxy.guid%type;
  s_rjhid brpms.tb_risk_fxy.guid%type;
begin
  for v_loop in 1 .. brpms.fxydata.v_row loop
    s_yjhid := brpms.fxydata.v_guid(v_loop);
    select (select f.guid from brpms.tb_risk_fxy f where f.tdjh_id = r.guid)
      into s_rjhid
      from (select * from brpms.tb_risk_tdjh where jhlx = 1) y,
          (select * from brpms.tb_risk_tdjh where jhlx = 2) r
    where y.yxtjhbh = r.yjhzj
      and exists
    (select fxy.guid
              from brpms.tb_risk_fxy fxy
            where fxy.tdjh_id = r.guid)
      and y.guid =
          (select f.tdjh_id from brpms.tb_risk_fxy f where f.guid = s_yjhid)
      and rownum = 1;
    if s_rjhid is not null then
      brpms.p_copy_y2r_manual(s_yjhid, s_rjhid);
    end if;
  end loop;
end;
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product