My colleague and I are attempting to write a database trigger in PL/SQL that can look into a mapping table to find the ID of a specific column from another table (example tables included below) and then update the value in that column.
We need to be able to do this as a BEFORE EACH ROW trigger so that we modify the data before it is committed into the database.
We ended up writing a massive CASE statement to handle this, but does anyone know of a better way to handle this sort of situation? In our real life example, we also have 70 columns to look through, rather than 8, like in the example below.
create table test_basis (
id number(22,0) not null,
item_1 number(22,0) not null,
item_2 number(22,0) not null,
item_3 number(22,0) not null,
item_4 number(22,0) not null,
item_5 number(22,0) not null,
item_6 number(22,0) not null,
item_7 number(22,0) not null,
item_8 number(22,0) not null
)
create table basis_mapping (
column_id number(22,0),
column_name varchar2(35)
)
insert into basis_mapping(1, 'Company');
insert into basis_mapping(2, 'Account');
insert into basis_mapping(3, 'Manager');
insert into basis_mapping(4, 'User');
For our purposes, we need to look up the column_id from basis_mapping based on the column_name, and then update the :new.item_### field in the current row. So, if we wanted to change the value in the 'User' column, we would run the following statement
select column_id
into l_basis_id
from basis_mapping
where column_name = 'User'
Now that we have 4 in the l_basis_id variable, how can we use this to generate a statement like the following?
:new.item_4 = 0