我想在Oracle上运行一个触发器,在更新事件之后用来自另一个表的数据更新表的多个字段。我想使用动态SQL语句。两个表都有很多共同的字段,前缀不同。仅当我正在更新的字段是显式的时,“立即执行”的使用才有效。一旦我使用变量作为字段名称,它就不起作用。任何的想法?
Here is the code :
create or replace TRIGGER AF_UPDATE_PRODUCT_REQUEST
AFTER UPDATE ON PRODUCT_REQUEST
REFERENCING OLD AS OLD NEW AS NEW
FOR 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;
FOR c1 in (SELECT column_name from user_tab_columns WHERE table_name='PRODUCT_REQUEST')
LOOP
old_value:=to_char(:old.PREQDESC2);
new_value:=to_char(:new.PREQDESC2);
IF old_value<>new_value THEN
product_fieldname:=replace(c1.column_name,'PREQ','PU');
select count(*) into counter from user_tab_columns WHERE table_name='PRODUCT' and column_name=product_fieldname;
IF counter=1 THEN
tata:=tata+1;
/*execute immediate 'update product set '|| product_fieldname ||'=:new.'|| c1.column_name ||' where pupname=:old.preqpname';*/
/*execute immediate 'update product set pushelflife=16 where pupname=:d3' using :old.preqpname;*/
IF product_fieldname='PUSHELFLIFE' THEN
/*execute immediate 'update product set pushelflife=:d2 where pupname=:d3' using 15,:old.preqpname;*/
execute immediate 'update product set :d1=:d2 where pupname=:d3' using product_fieldname,15,:old.preqpname;
END IF;
END IF;
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS THEN NULL;
END;
答案 0 :(得分:0)
您不能将对象或列名称作为绑定变量传递给动态SQL语句。您必须使用这些列名构造动态SQL语句。像
这样的东西execute immediate 'update product ' ||
' set ' || product_fieldname || ' = :val '
' where pupname = :key'
using 15, :old.preqpname