In an Oracle trigger, can I assign new and old to a rowtype variable?

后端 未结 2 1865
一向
一向 2020-12-06 22:08

I\'ve been tasked with implementing several trigger scripts, and I\'ve got examples from coworkers to work from. In those, they use pl-sql conditionals for updating/insertin

2条回答
  •  庸人自扰
    2020-12-06 22:39

    I've decided to answer on this question as I'd heard it many times and never seen any solution.

    So the idea is to have one generic "structure independent" trigger calling some PL/SQL procedure passing entire new and old row.

    I used object table for that.

    create or replace type test_table_type as object(
      ... your attributes ....
    );
    
    create table test of test_table_type;
    
    create or replace trigger trg_test_upd
    before update on test
    for each row
      l_new_row  test_table_type;
      l_old_row  test_table_type;
      l_row_id   raw(40000);
    begin
      l_new_row := :new.sys_nc_rowinfo$; -- <-- this is your new row
      l_old_row := :old.sys_nc_rowinfo$; -- <-- this is your old row
      l_row_id :=  :old.sys_nc_oid$; -- <-- this your row logical ID (not rowid!!!). You can use it if you don't have primary key or it is composite.
      -- here you can do what ever you want with you old/new rows.
      ...
    end;
    /
    

    I like this method as it allows to define common attributes in base type and inherit other types from this type. So you can use generic code even for many tables based on same base type.

提交回复
热议问题