Trigger ½ÇÇà½Ã Ora-04091:Mutation error 󸮹æ¹ý
ÀÛ¼ºÀÚ °ü¸®ÀÚ ÀÛ¼º½Ã°£ 2008-01-10 13:28:22
 

Trigger ½ÇÇà½Ã Ora-04091:Mutation error 󸮹æ¹ý 


¿¹¸¦µé¾î emp Table¿¡ Insert/Update/Delete µÉ¶§¸¶´Ù ºÎ¼­º° Æò±Õ±Þ¿© Table¿¡ Update ÇÑ´Ù°í °¡Á¤ÇßÀ»¶§ À̶§ »ç¿ëÀÚ°¡ emp Table¿¡ º¯°æÀÌ °¡ÇØ Á³À»¶§ emp Table¿¡ ¾î¶°ÇÑ Ã³¸®¸¦ ÇÏ´Â Trigger¸¦ ¸¸µç´Ù¸é ¿øÄ¡¾ÊÀº °á°ú°¡ ³ª¿Ã¼ö°¡ ÀÖ°ÚÁÒ.
±×·¡¼­ OracleÀº emp Table¿¡ row Trigger¸¦ ¸¸µé¸é ¿øõÀûÀ¸·Î emp TableÀ» ¾Æ¿¹ AccessÇÒ¼ö ¾ø°Ô Çϸç ÀÌ¿Í °°Àº ¿øÄ¢¿¡ À§¹èµÉ¶§ mutationg error°¡ ¹ß»ýÇÏ°Ô µÈ´Ù.

ÇØ°áÃ¥Àº Statement trigger·Î ¸¸µé´øÁö PL/SQL TableÀ» »ý¼ºÇÏ¿© ÇØ°áÇÏ´Â ¹æ¹ýÀÌ ÀÖ´Ù. Statement Trigger ÀÎ °æ¿ì¿¡´Â :new.column, :old.columnÀÇ »ç¿ëÀº ºÒ°¡´ÉÇÏ´Ù. ¿©±â¼­´Â ÈÄÀÚ·Î ÇØ°áÇغ¸ÀÚ.

emp TableÀÇ LayOutÀº ¾Æ·¡¿Í °°´Ù.
SQL>desc emp
  NAME                      NULL?            TYPE
  ------------------------------------------------
  empno
  ename
  job
  mgr
  sal
  deptno

SQL>desc dept
  NAME                      NULL?            TYPE
  ------------------------------------------------
  deptno
  dname
  loc
  sal

1. PL/SQL TableÀ» »ý¼ºÇÑ´Ù.
create or replace package emp_pkg as
  type  emp_tab_type is table of emp.deptno%type index by
        binary_integer;
  emp_old  emp_tab_type;
  emp_new  emp_tab_type;
  emp_index binary_integer;
end emp_pkg;

2. Before statement Trigger¸¦ »ý¼ºÇÑ´Ù.
create or replace trigger emp_bef_stm_all
      before insert or update or delete on emp
begin
emp_pkg.emp_index  := 0;
end;
/

3. After row Trigger¸¦ »ý¼ºÇÑ´Ù.
create or replace trigger emp_aft_row_all
      after insert or update or delete on emp
      for each row
begin
emp_pkg.emp_index    := emp_pkg.emp_index + 1;
emp_pkg.emp_old(emp_pkg.emp_index) := :old.deptno;
emp_pkg.emp_new(emp_pkg.emp_index) := :new.deptno;
end;
/

4. After Statement Trigger¸¦ »ý¼º
create or replace trigger emp_aft_atm_all
      after insert or update or delete on emp
      for each row
declare
v_sal  emp.sal%type;
begin
for i in 1..emp_pkg.emp_index loop
    select avg(sal) into v_sal from emp
    where deptno = emp_pkg.emp_old(i);

    update dept set sal = v_sal
    where deptno = emp_pkg.emp_old(i);

    dbms_output.put_line('DEPTNO(old)=>'||to_char(emp_pkg.emp_old(i)));

    if emp_pkg.emp_new(i) != emp_pkg.emp_old(i) then
        select avg(sal) into v_sal from emp
        where deptno = emp_pkg.emp_new(i);

        update dept set sal = v_sal
        where deptno = emp_pkg.emp_new(i);

        dbms_output.put_line('DEPTNO(NEW)=>'||to_char(emp_pkg.emp_NEW(i)));
    end if;
end loop;
emp_pkg.emp_index := 0;
end;
/

5. È®ÀÎ
SQL>update emp set sap = 90000
  where empno = 1111;

SQL>
  DEPTNO(old)=>20
  1 row updated.


¸ñ·Ï | ÀÔ·Â | ¼öÁ¤ | ´äº¯ | »èÁ¦