Oracle (11g) compound trigger not updating CLOB data field

你离开我真会死。 提交于 2019-12-11 04:50:07

问题


I tried searching for answers to this but couldn't really find anything useful. Either there isn't or my searching capabilities took a knock. Regardless, here's my situation:

I have a case where I've got 2 identical tables, Z_TEST and Z_TEST2. Call Z_TEST2 an audit table if you'd like.

Both have 2 columns, ID (number) and CFIELD (CLOB). Z_TEST has a trigger which will either insert or update Z_TEST2 (in effect only the CLOB field).

Scenario 1:

If I create the following trigger on Z_TEST, which is a normal trigger, the audit table gets updated too:

CREATE OR REPLACE TRIGGER Z_TEST_TRIG


AFTER INSERT OR UPDATE ON Z_TEST
    FOR EACH ROW

Begin
    If inserting then
    Begin
        insert into Z_TEST2 values(:new.ID, :new.CFIELD);
    end;
  end if;

  if updating then
    begin
        update Z_TEST2 Set CFIELD = :new.CFIELD where ID = :new.id;
    end;
  end if;
End;

Scenario 2:

If I create the following trigger which is a compound trigger and make use of its "After each row" block, the audit table's CLOB updates with nothing, null:

create or replace trigger Z_TEST_TRIG
FOR UPDATE OR INSERT ON Z_TEST
COMPOUND TRIGGER 

AFTER EACH ROW IS
Begin
    If inserting then
    Begin
        insert into Z_TEST2 values(:new.ID, :new.CFIELD);
    end;
  end if;

  if updating then

    begin
        update Z_TEST2 Set CFIELD = :new.CFIELD where ID = :new.id;
    end;
  end if;
END AFTER EACH ROW;

END Z_TEST_TRIG;

Does anyone have any idea why this would work for scenario 1, but not 2? Our WHOLE framework is based on scenario 2 (compound triggers) and I've only recently come across the need to use CLOB data types, and with it came this problem.

Why the difference and is my code missing something?


回答1:


Having no time to linger on this issue I solved it by making use of a variable.

Declaring a CLOB variable in the declaration section and assigning the value of the :new.clob_field to it either in BEFORE EACH ROW, or AFTER EACH ROW, and using the variable in your insert/update statement rather than :new.clob_field within the trigger solves this issue.

I came across a lot of posts by people battling with this (compound triggers specifically, not simple triggers), so I hope the time i spent on this helps someone else and saves them time.

It would really be helpful to my sanity if anyone comes across this post who knows the reason why :new.clob_field loses its value in a compound trigger when used in insert/update statements in the BEFORE/AFTER each row section. It would be awful dying one day with this thought stuck in my mind...

I'll also make the assumption that this would work for BLOB as well (if that causes an issue).




回答2:


Something like this.... you just need to add update clause

   CREATE OR REPLACE TRIGGER after_row_insert
    AFTER INSERT
    ON Z_TEST
    REFERENCING NEW AS NEW OLD AS OLD
    FOR EACH ROW
    Begin
     insert into Z_TEST2 values(:new.ID, :new.CFIELD);
    End;


来源:https://stackoverflow.com/questions/16982744/oracle-11g-compound-trigger-not-updating-clob-data-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!