【问题标题】:PLSQL: BEFORE INSERT TRIGGER WHERE CONDITION AND UPDATE NOT WORKINGPLSQL:在条件和更新不起作用的地方插入触发器之前
【发布时间】:2020-10-07 18:19:55
【问题描述】:

我的代码不起作用。没有错误 要求: 我想在 TABLE1 上创建一个触发器(在插入之前或之后)以更新 TABLE1.TABLETYPE=1 列并将 TABLE1.E2B2 XML 标记值更新为 1(/icstable/safetytable/tabletype)
注意:TABLE1.E2B2 是 clob 并存储 XML 文件。
有两个问题:1)“WHEN TRIGGER”条款不起作用
2)当我删除时触发子句时,更新也没有发生。

对于表 1 中 TABLE1.AGENCY_ID=12345 和 TABLE1.TABLETYPE=2

的所有记录
CREATE OR REPLACE TRIGGER TRG_SAF_BEF_INS BEFORE
  INSERT ON TABLE1 REFERENCING NEW AS NEW OLD AS OLD  FOR EACH ROW 
  WHEN (NEW.AGENCY_ID  = 12345 AND NEW.TABLETYPE=2) 
  DECLARE 
  v_error_code         NUMBER;
  v_rep_id             NUMBER;
     
  BEGIN
           
   
   --Updating TABLE1.TABLETYPE  to 1    
          
      :NEW.TABLETYPE:=1;

    --updating the TABLE1.E2B2 Which is XML File stored in CLOB  
      select updatexml( xmltype.createxml(:NEW.E2B2),'/icstable/safetytable/tabletype/text()',1  ).getClobVal() 
      INTO :NEW.E2B2  FROM DUAL;
      
    
  EXCEPTION
  WHEN OTHERS THEN
    
     RAISE_APPLICATION_ERROR(-20004, 'ERROR');
  
END TRG_SAF_BEF_INS;

创建表是这样的:

create table table1 (agency_id number,tabletype number,e2b2 clob);      

The data looks below:     
**********************************************
||agency_id ||  tabletype ||    e2b2 ||                                                        
**********************************************
12345   2    "<icstable>
  <safetytable>
  <safetytableid>E3223</safetytableid> 
  <transmissiondater>20201007105128-0700</transmissiondater> 
  <tabletype>2</tabletype> 
  </icstable>
  </safetytable>"                
22332   4   "<icstable>
  <safetytable>
  <safetytableid>E5623</safetytableid> 
  <transmissiondater>20201007105128-0700</transmissiondater> 
  <tabletype>4</tabletype> 
  </icstable>
  </safetytable>"
45226   3   "<icstable>
  <safetytable>
  <safetytableid>E3823</safetytableid> 
  <transmissiondater>20201007105128-0700</transmissiondater> 
  <tabletype>3</tabletype> 
  </icstable>
  </safetytable>"
12345   2   "<icstable>
  <safetytable>
  <safetytableid>E3333</safetytableid> 
  <transmissiondater>20201007105128-0700</transmissiondater> 
  <tabletype>2</tabletype> 
  </icstable>
  </safetytable>"
12345   1   "<icstable>
  <safetytable>
  <safetytableid>E322w2</safetytableid> 
  <transmissiondater>20201007105128-0700</transmissiondater> 
  <tabletype>1</tabletype> 
  </icstable>
  </safetytable>"
                                                             

【问题讨论】:

  • “不工作”不是 Oracle 错误。我建议你删除这个 useless 异常处理部分,让 Oracle 报错。
  • 谢谢小脚,当我说不工作时,我的意思是更新没有发生。问题是为什么 WHEN 触发条件不起作用?以及更改未更新的原因。
  • 你能发布一个测试用例吗? CREATE TABLE + INSERT INTO 以便我们自己查看?
  • 创建表 TABLE1 (TABLETYPE NUMBER,E2B2 CLOB);
    表类型值为 1,2,3,4,5,6。
    E2B2 值如下所示
    E382320201007105128-07002

标签: oracle plsql triggers


【解决方案1】:

表和触发器:

SQL> create table table1 (tabletype number,e2b2 clob);

Table created.

SQL> create or replace trigger trg_test
  2    before insert on table1
  3    for each row
  4  begin
  5    select updatexml( xmltype.createxml(:new.E2B2),'/icstable/safetytable/tabletype/text()',1  ).getClobVal()
  6    into :new.e2b2
  7    from dual;
  8  end;
  9  /

Trigger created.

测试:

SQL> insert into table1
  2  select 1,
  3  '<icstable>   <safetytable>     <safetytableid>E3823</safetytableid>     <transmissiondater>20201007105128-0700</transmissiondater>     <tabletype>2</tabletype>   </safetytable></icstable>'
  4  from dual;                                                                                                                             -- inserted 2

    1 row created.
    
SQL> select * from table1;

 TABLETYPE
----------
E2B2
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

         1
<icstable><safetytable><safetytableid>E3823</safetytableid><transmissiondater>20201007105128-0700</transmissiondater><tabletype>1</tabletype></safetytable></icstable>
                                                                                                                  -- results in 1


SQL>

如果你向右滚动,你会看到

  • 2INSERT 声明的一部分
  • 触发器将其更改为1

【讨论】:

  • 很抱歉创建表是这样的:create table table1 (agency_id number,tabletype number,e2b2 clob);
  • 问题是 ONLY BEFORE INSERT 在我的情况下不起作用,因为我正在检查的数据不存在,所以我使用了 BEFORE OR UPDATE,然后 when 子句也起作用并且更新也是感谢@littlefoot 为您提供宝贵的时间......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-30
  • 1970-01-01
  • 2013-06-11
  • 1970-01-01
相关资源
最近更新 更多