如何在oracle

时间:2016-07-27 12:57:28

标签: sql oracle triggers oracle12c beforeupdate

我搜索了我的问题,但还没有得到任何解决方案,所以我在这里发布。我正在使用Oracle Database 12c企业版12.1.0.1.0版 - 64位生产。

我有一个Main_Table,假设有10个记录,我有一个Log_Table来记录(插入)更新新表时的旧值和新值。

我正在使用复合触发器(以避免变异错误)循环遍历" Main_Table"的所有列。通过new过滤动态获取更新行记录。主键(UID))。

我希望我正在使用复合触发器。

我没有使用:old:new,因为我正在动态循环遍历所有列,并且需要匹配列值。

But it is again giving me mutating error:
Error report -
SQL Error: ORA-04091: table Main_Table  is mutating, trigger/function may not see it
ORA-06512: at "Schema.TRG_TEST", line 87
ORA-04088: error during execution of trigger 'Schema.TRG_TEST'
04091. 00000 -  "table %s.%s is mutating, trigger/function may not see it"
*Cause:    A trigger (or a user defined plsql function that is referenced in
           this statement) attempted to look at (or modify) a table that was
           in the middle of being modified by the statement which fired it.
*Action:   Rewrite the trigger (or function) so it does not read that table.


Below is my trigger code:

create or replace TRIGGER TRG_TEST
 FOR INSERT or UPDATE ON Main_Table
COMPOUND TRIGGER
  TYPE NUMBER_TABLE IS TABLE OF NUMBER;
  tblTABLE2_IDS  NUMBER_TABLE;
    TYPE VARCHAR_TABLE IS TABLE OF VARCHAR(2000);
  tblTABLE3_IDS  VARCHAR_TABLE;
   TYPE VARCHAR_TABLE1 IS TABLE OF VARCHAR(2000);
  tblTABLE4_IDS  VARCHAR_TABLE1;

  vcount NUMBER;
  colCount NUMBER;
   colCountAfter NUMBER;
  vvalue VARCHAR2(4000);
  vcolumn VARCHAR2(4000);
  sql1 VARCHAR2(4000);
  dynamicq varchar(4000);
testv varchar(2000);
testv1 varchar(2000);
ssql varchar(2000);
ssql1 varchar(2000);
maxsiteid NUMBER;
newsid varchar(2000);
newstid varchar(2000);
newuid varchar(2000);
 --Executed before DML statement

  BEFORE STATEMENT IS
  BEGIN
 tblTABLE2_IDS := NUMBER_TABLE();
    tblTABLE3_IDS:=  VARCHAR_TABLE();
    tblTABLE4_IDS:=  VARCHAR_TABLE1();
     IF UPDATING THEN
     dbms_output.put_line('Before Statement - In Updating'); 
         --dbms_output.put_line('Before Each Row - In Updating'); 
        -- tblTABLE2_IDS.EXTEND;
      --tblTABLE2_IDS(tblTABLE2_IDS.LAST) := :new.UID;
      END IF;
  END BEFORE STATEMENT;

  --Executed before each row change- :NEW, :OLD are available
     BEFORE EACH ROW IS
     BEGIN
        IF UPDATING THEN
         dbms_output.put_line('Before Each Row - In Updating'); 
         tblTABLE2_IDS.EXTEND;
      tblTABLE2_IDS(tblTABLE2_IDS.LAST) := :new.UID;
     -- FOR columnlist IN
      --(SELECT COLUMN_NAME AS COLUMN_NAME  FROM all_tab_columns  WHERE lower(TABLE_NAME) = 'Main_Table'
      -- AND lower(COLUMN_NAME) NOT IN ( 's_id' ,'msid' ,'st' ,'u_id' ,'db_flag' )) 

      --LOOP
      --colCount:=colCount+1;
      --ssql1:='select '||columnlist.COLUMN_NAME||' from Main_Table where UID='||tblTABLE2_IDS(tblTABLE2_IDS.LAST)||'';
      --dbms_output.put_line(ssql1);
      --execute immediate ssql1 into testv;
      --tblTABLE3_IDS(colCount):=testv;
      --dbms_output.put_line(testv);
      --END LOOP;
           END IF;
     END BEFORE EACH ROW;

 --Executed aftereach row change- :NEW, :OLD are available
  AFTER EACH ROW IS
  BEGIN

    IF UPDATING THEN
         dbms_output.put_line('After Each Row - In Updating'); 
      FOR columnlist IN
      (SELECT COLUMN_NAME AS COLUMN_NAME  FROM all_tab_columns  WHERE lower(TABLE_NAME) = 'Main_Table'
       AND lower(COLUMN_NAME) NOT IN ( 's_id' ,'msid' ,'st' ,'u_id' ,'db_flag' )) 

      LOOP
      colCount:=colCount+1;
      ssql1:='select '||columnlist.COLUMN_NAME||' from Main_Table where UID='||tblTABLE2_IDS(tblTABLE2_IDS.LAST)||'';
      dbms_output.put_line(ssql1);
      execute immediate ssql1 into testv;
      tblTABLE3_IDS(colCount):=testv;
      dbms_output.put_line(testv);
      END LOOP;
           END IF;
  END AFTER EACH ROW;

--Executed after DML statement
  AFTER STATEMENT IS
  BEGIN

      IF UPDATING THEN
            dbms_output.put_line('After Statement - In Updating'); 
          FOR columnlist IN
      (SELECT COLUMN_NAME AS COLUMN_NAME  FROM all_tab_columns  WHERE lower(TABLE_NAME) = 'Main_Table'
       AND lower(COLUMN_NAME) NOT IN ( 's_id' ,'msid' ,'st' ,'u_id' ,'db_flag' )) 

      LOOP
      colCountAfter:=colCountAfter+1;
      dbms_output.put_line('loop started'); 

       ssql1:='select '||columnlist.COLUMN_NAME||' from Main_Table where UID='||tblTABLE2_IDS(tblTABLE2_IDS.LAST)||'';

      execute immediate ssql1 into testv1;
      dbms_output.put_line(testv);
      tblTABLE3_IDS(colCountAfter):=testv1;

       IF ((testv) IS NOT NULL) THEN
       FOR i IN tblTABLE3_IDS.FIRST..tblTABLE2_IDS.LAST LOOP
       dbms_output.put_line('Values No :' ||i||' is ' || tblTABLE3_IDS(i) || ' and ' ||tblTABLE4_IDS(i));
       IF(tblTABLE3_IDS(i)=tblTABLE4_IDS(i)) THEN
         dbms_output.put_line(testv1);


       ELSE
       -- dbms_output.put_line('select :new.'|| columnlist.COLUMN_NAME||' from dual');
        dbms_output.put_line(testv1);           


         INSERT INTO Log_Table
              (
                user_id,  
                log_action,
                log_table_name,
                schema_name,
                log_column_name,
                col_old_val,
                col_new_val,
                ne_type,
                ne_id,
                system_id
              )
              VALUES
              (
                newuid,
                'UPDATE',
                'Main_Table',
                'SCHEMA'
                ,columnlist.COLUMN_NAME
                ,tblTABLE3_IDS(i)
                ,tblTABLE4_IDS(i)
                ,'S'
                ,newstid
                ,newsid
              );
       END IF;
       END LOOP;


       END IF;
      END LOOP;
           END IF;

  END AFTER STATEMENT;
  END TRG_TEST;

最初我曾尝试访问"每行之前的更新表"然后我尝试在" After Each Row"中访问它,在两种情况下都是相同的错误。

即使在使用复合触发器之后,我也在努力寻找解决方案,但是我对插入实现了相同的效果。

任何人都可以帮助实现它。在此先感谢。

2 个答案:

答案 0 :(得分:0)

通常,after语句行级触发器足以填充日志记录表。 旧的和新的列值可用于:old和:new伪记录。

一个非常基本的例子:

create or replace trigger my_trigger
   after insert or update on my_table
   for each row
declare

    begin

       if inserting
       then
          insert into logging table
             (id
             ,my_column_old
             ,my_column_new)
          values
             (:new.id
             ,null      -- no old value in case of insert
             ,:new.my_column);
       else
          insert into logging table
             (id
             ,my_column_old
             ,my_column_new)
          values
             (:new.id
             ,:old.my_column
             ,:new.my_column);
       end if;
    end;

答案 1 :(得分:0)

据我所知,您希望跟踪新添加列的跟踪历史记录(用户可以添加和删除列),并且您不需要重新编译触发器。

然后您可以使用Oracle Total Recall

很高兴你有Oracle 12c EE(大多数错误是修复的)。