我搜索了我的問題,但無法獲得任何解決方案,所以我在這裏發佈它。我正在使用Oracle Database 12c企業版版本12.1.0.1.0 - 64位生產。如何在oracle中的複合觸發更新中獲取舊值
我有一個Main_Table,假設有10條記錄,我有一個Log_Table記錄(插入)新表更新時的舊值和新值。
我正在使用複合觸發器(以避免變異錯誤)動態地循環遍歷「Main_Table」的所有列,並通過新的過濾來獲取更新的行記錄。 Primarykey(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;
最初我曾試圖訪問更新表中「各行之前」然後我試圖訪問它,同樣的錯誤在這兩種情況下,「每一行後」。
即使在使用複合觸發器後,我仍然很難找到解決方案,但是對於插入操作我也達到了同樣的效果。
任何人都可以幫助如何實現它。提前致謝。
請發佈數據庫版本和版本。我假設你重新輪流。有一些Oracle產品也是這樣做的。 –
我正在使用Oracle Database 12c企業版版本12.1.0.1.0 - 64位生產。 – nks
不,只是,沒有。在運行時爲每個單獨的DML語句生成,編譯和運行動態SQL *的想法非常糟糕。如果您必須使用觸發器來跟蹤所有更改,請分別爲每個表生成觸發器代碼。你會爲自己節省很多痛苦和麻煩,相信我。 –