EXECUTE IMMEDIATE使用变量列更新查询

时间:2017-06-13 11:53:23

标签: sql oracle dynamic-sql

我第一次使用命令EXECUTE IMMEDIATE,我想问你是否可以帮助我。

我有一个变量列,因为我在互联网上搜索并找到了EXECUTION IMMEDIATE命令,但它给了我以下错误:

  

ORA-00904:" ACTUATION":无效的标识符
  ORA-06512:第9行

我真的很困惑,因为ACTUATION是" old_val"列而不是" column_name"专栏也许你可以帮助我。这是代码:

declare
cursor such is 
select column_name, old_val, CTN.ID from abc.firsttable
ctn, def.secondtable#CC ctncc
where CTN.bup#rev = CTNCC.bup#rev
and ID in (127605689)
and CTN.bup#changefrom > to_date ('08/06/2017', 'dd/MM/YYYY');
begin
for srec in such loop 
EXECUTE IMMEDIATE
'update firsttable ctn2
set ctn.' || srec.column_name || ' = '|| srec.old_val ||' 
where CTN2.ID = '|| srec.id;
end loop; 
end;

2 个答案:

答案 0 :(得分:4)

试试这个:

EXECUTE IMMEDIATE
   'update firsttable ctn2
    set ctn.' || srec.column_name || ' = :OldVal 
    where CTN2.ID = :id' 
using srec.old_val, srec.id;

答案 1 :(得分:1)

我想是的,你必须在字符串的参数之间添加更多'

显示此信息,在您的代码中,我在'

之间添加了3 || srec.old_val ||
EXECUTE IMMEDIATE
'update firsttable ctn2
set ctn.' || srec.column_name || ' = '''|| srec.old_val ||'''
where CTN2.ID = '|| srec.id;
end loop; 
end;