我想写一个触发器,用来记录用户对表的操作,包括能记录操作的表名,操作类型,以及操作的前后的新旧值.
当然这个触发器要能针对数据库里的每个表,也就是说这个触发器要有可移植性,我只是需要换下on tab_name 和触发器的名称就能 针对另一个表建立相同的触发器.
好像需要写一个function的..

解决方案 »

  1.   

    create or replace trigger trgRollbackDeviceTransfer
      after update on asset_transfer_interface  
      for each row
    declare
      v_orig_dept varchar2(20);
      v_from_table varchar2(50);
    begin
      if UPDATING('process_status') then
         if :NEW.process_status = 'REJECT' then
            --本次转移被拒绝
            v_from_table := UPPER(trim(:NEW.description));
            if v_from_table='LBS_DEPT_INFO' then
               v_orig_dept := 'LBS_'||:NEW.dept_no_from;
            elsif v_from_table='GBS_DEPT_INFO' then
               v_orig_dept := 'GBS_'||:NEW.dept_no_from;
            else
               v_orig_dept := :NEW.dept_no_from;   
            end if;
            update devicebasicinfo set depid = v_orig_dept
            where deviceid = :NEW.deviceid;
         elsif :NEW.process_status = 'UPLOAD' then
            null;
         else      
             --本次转移完成
            update device_interface_log set process_flag='1'
            where deviceid=:NEW.deviceid and process_type='T' 
            and interface_id = :NEW.transfer_interface_id;     
         end if;     
      end if;
    end;
    /举个例子
      

  2.   

    after update on database
      

  3.   

    for ct in(select table_name from user_tables)
    loop
      vsql = 'create or replace trigger TA_'||ct.table_name||'
      after update on '||ct.table_name||'
      for each row
    .....
    ';
      for ct in(select column_name from User_Tab_Cols where column_name =ct.table_name)
      loop
        vsql =   vsql +....
      end loop;
      vsql =   vsql +....
      EXECUTE IMMEDIATE vs_sql;
    end loop;