oracle表執行更新操作的時候,那些欄位被更新了
問題要求:每次表執行更新操作的時候,要知道是那些欄位被更新了?
解決辦法:1、在表上建立觸發器,先取得表中有那些欄位,可以執行一下語句:
ORACLE:
SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME = '表名' ORDER BY COLUMN_ID;
或
SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS WHERE TABLE_NAME = '表名' ORDER BY COLUMN_ID;
注意:表明要區分大小寫。
SQLSERVER:
select name from syscolumns where id=object_id('表名');
或
select count(*) from syscolumns where id=object_id('表名');
2、迴圈欄位,看看那些欄位被更新了。
迴圈用for,判斷那些欄位被更新了,用updating(欄位名);
3、觸發器完整程式碼如下
CREATE OR REPLACE TRIGGER TB_TASK_TRGGER
AFTER UPDATE
ON TB_TASK
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
DECLARE
vrupdate varchar2(100);
BEGIN
vrupdate:='';
FOR TEMPCOLUME IN (SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'TB_TASK' ORDER BY COLUMN_ID)
LOOP
IF UPDATING(TEMPCOLUME.COLUMN_NAME)
THEN
dbms_output.put_line(TEMPCOLUME.COLUMN_NAME);
vrupdate:=vrupdate || ',';
END IF;
END LOOP;
UPDATE USERLIST SET duty=vrupdate where userid='1000000386';
EXCEPTION
WHEN OTHERS
THEN
RAISE;
END;