sql - Dynamic statement in a trigger -
i want run trigger on oracle update several fields of table data coming table, after update event. want use dynamic sql statements. both tables have lot of fields in common, different prefix. use of "execute immediate" works if field i'm updating explicit. use variable field name, doesn't work. idea?
here code : create or replace trigger af_update_product_request after update on product_request referencing old old new new each row when (old.preqcomplete=1) declare product_fieldname varchar2(100); counter number(1); tata number(3); old_value varchar2(500); new_value varchar2(500); begin tata:=0; c1 in (select column_name user_tab_columns table_name='product_request') loop old_value:=to_char(:old.preqdesc2); new_value:=to_char(:new.preqdesc2); if old_value<>new_value product_fieldname:=replace(c1.column_name,'preq','pu'); select count(*) counter user_tab_columns table_name='product' , column_name=product_fieldname; if counter=1 tata:=tata+1; /*execute immediate 'update product set '|| product_fieldname ||'=:new.'|| c1.column_name ||' pupname=:old.preqpname';*/ /*execute immediate 'update product set pushelflife=16 pupname=:d3' using :old.preqpname;*/ if product_fieldname='pushelflife' /*execute immediate 'update product set pushelflife=:d2 pupname=:d3' using 15,:old.preqpname;*/ execute immediate 'update product set :d1=:d2 pupname=:d3' using product_fieldname,15,:old.preqpname; end if; end if; end if; end loop; exception when others null; end;
you cannot pass in object or column names bind variables dynamic sql statement. have construct dynamic sql statement using column names. like
execute immediate 'update product ' || ' set ' || product_fieldname || ' = :val ' ' pupname = :key' using 15, :old.preqpname
Comments
Post a Comment