问题要求:每次表执行更新操作的时候,要知道是那些字段被更新了?
解决办法: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;