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
No, you can't. :new
and :old
are pseudo-records, not actual records that you can assign to a local variable. If your tables were based on object types, :new
and :old
would then be actual instances of the particular object type that can be passed around like any other object. But it's pretty unlikely that it is worth defining your tables in terms of objects just to make your triggers easier to write.
You could, of course, write a PL/SQL package that automatically generates the trigger code you want by doing things like querying the data dictionary (i.e. all_tab_columns
to get the list of columns in a table) ans using dynamic SQL. Depending on the number of triggers you expect to have to write, this may be easier than writing and maintaining a ton of similar code.
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.