auditing 50 columns using oracle trigger

后端 未结 5 858
故里飘歌
故里飘歌 2020-12-11 20:08

I need to create a trigger in oracle 11g for auditing a table .

I have a table with 50 columns that need to be audited

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-11 20:38

    Your immediate problem with the else always being called is because you're using your index variable r directly, rather than looking up the relevant column name:

    for r in v_tab_col_nt.first..v_tab_col_nt.last
    loop
        if updating(v_tab_col_nt(r)) then
            insert into data_table values(1,'i am updating '||v_tab_col_nt(r));
        else
            insert into data_table values(2,'i am inserting '||v_tab_col_nt(r));
        end if;
    end loop;
    

    You're also only showing an id column in your table creation, so when r is 2, it will always say it's inserting name, never updating. More importantly, if you did have a name column and were only updating that for a given id, this code would show the id as inserting when it hadn't changed. You need to split the insert/update into separate blocks:

    if updating then
        for r in v_tab_col_nt.first..v_tab_col_nt.last loop
            if updating(v_tab_col_nt(r)) then
                insert into data_table values(1,'i am updating '||v_tab_col_nt(r));
            end if;
        end loop;
    else /* inserting */
        for r in v_tab_col_nt.first..v_tab_col_nt.last loop
            insert into data_table values(2,'i am inserting '||v_tab_col_nt(r));
        end loop;
    end if;
    

    This will still say it's inserting name even if the column doesn't exist, but I assume that's a mistake, and I guess you'd be trying to populate the list of names from user_tab_columns anyway if you really want to try to make it dynamic.


    I agree with (at least some of) the others that you'd probably be better off with an audit table that takes a copy of the whole row, rather than individual columns. Your objection seems to be the complication of individually listing which columns changed. You could still get this information, with a bit of work, by unpivoting the audit table when you need column-by-column data. For example:

    create table temp12(id number, col1 number, col2 number, col3 number);
    create table temp12_audit(id number, col1 number, col2 number, col3 number,
        action char(1), when timestamp);
    
    create or replace trigger temp12_trig
    before update or insert on temp12
    for each row
    declare
        l_action char(1);
    begin
        if inserting then
            l_action := 'I';
        else
            l_action := 'U';
        end if;
    
        insert into temp12_audit(id, col1, col2, col3, action, when)
        values (:new.id, :new.col1, :new.col2, :new.col3, l_action, systimestamp);
    end;
    /
    
    insert into temp12(id, col1, col2, col3) values (123, 1, 2, 3);
    insert into temp12(id, col1, col2, col3) values (456, 4, 5, 6);
    update temp12 set col1 = 9, col2 = 8 where id = 123;
    update temp12 set col1 = 7, col3 = 9 where id = 456;
    update temp12 set col3 = 7 where id = 123;
    
    select * from temp12_audit order by when;
    
            ID       COL1       COL2       COL3 A WHEN
    ---------- ---------- ---------- ---------- - -------------------------
           123          1          2          3 I 29/06/2012 15:07:47.349
           456          4          5          6 I 29/06/2012 15:07:47.357
           123          9          8          3 U 29/06/2012 15:07:47.366
           456          7          5          9 U 29/06/2012 15:07:47.369
           123          9          8          7 U 29/06/2012 15:07:47.371
    

    So you have one audit row for each action taken, two inserts and three updates. But you want to see separate data for each column that changed.

    select distinct id, when,
        case
            when action = 'I' then 'Record inserted'
            when prev_value is null and value is not null
                then col || ' set to ' || value
            when prev_value is not null and value is null
                then col || ' set to null'
            else col || ' changed from ' || prev_value || ' to ' || value
        end as change
    from (
        select *
        from (
            select id,
                col1, lag(col1) over (partition by id order by when) as prev_col1,
                col2, lag(col2) over (partition by id order by when) as prev_col2,
                col3, lag(col3) over (partition by id order by when) as prev_col3,
                action, when
            from temp12_audit
        )
        unpivot ((value, prev_value) for col in (
            (col1, prev_col1) as 'col1',
            (col2, prev_col2) as 'col2',
            (col3, prev_col3) as 'col3')
        )
    )
    where value != prev_value
        or (value is null and prev_value is not null)
        or (value is not null and prev_value is null)
    order by when, id;
    
            ID WHEN                      CHANGE
    ---------- ------------------------- -------------------------
           123 29/06/2012 15:07:47.349   Record inserted
           456 29/06/2012 15:07:47.357   Record inserted
           123 29/06/2012 15:07:47.366   col1 changed from 1 to 9
           123 29/06/2012 15:07:47.366   col2 changed from 2 to 8
           456 29/06/2012 15:07:47.369   col1 changed from 4 to 7
           456 29/06/2012 15:07:47.369   col3 changed from 6 to 9
           123 29/06/2012 15:07:47.371   col3 changed from 3 to 7
    

    The five audit records have turned into seven updates; the three update statements show the five columns modified. If you'll be using this a lot, you might consider making that into a view.

    So lets break that down just a little bit. The core is this inner select, which uses lag() to get the previous value of the row, from the previous audit record for that id:

            select id,
                col1, lag(col1) over (partition by id order by when) as prev_col1,
                col2, lag(col2) over (partition by id order by when) as prev_col2,
                col3, lag(col3) over (partition by id order by when) as prev_col3,
                action, when
            from temp12_audit
    

    That gives us a temporary view which has all the audit tables columns plus the lag column which is then used for the unpivot() operation, which you can use as you've tagged the question as 11g:

        select *
        from (
            ...
        )
        unpivot ((value, prev_value) for col in (
            (col1, prev_col1) as 'col1',
            (col2, prev_col2) as 'col2',
            (col3, prev_col3) as 'col3')
        )
    

    Now we have a temporary view which has id, action, when, col, value, prev_value columns; in this case as I only have three columns, that has three times the number of rows in the audit table. Finally the outer select filters that view to only include the rows where the value has changed, i.e. where value != prev_value (allowing for nulls).

    select
        ...
    from (
        ...
    )
    where value != prev_value
        or (value is null and prev_value is not null)
        or (value is not null and prev_value is null)
    

    I'm using case to just print something, but of course you can do whatever you want with the data. The distinct is needed because the insert entries in the audit table are also converted to three rows in the unpivoted view, and I'm showing the same text for all three from my first case clause.

提交回复
热议问题