R
R
Raushka2021-06-01 17:02:46
Oracle
Raushka, 2021-06-01 17:02:46

How to update data from one table to another table through a loop using dynamic sql?

All hello guys!
Can you help in solving the problem (It is necessary to update the data in the table tablenew table from the general_table table)

declare
  tablename varchar(255) := 'CoolTable';
tablenew varchar(255);
BEGIN
  select functable (tablename) into tablenew from dual;  --возвращает наличие таблицы 
  for rec in (select g.acode, g.clcode  from general_table g ) loop
 if tablename is not null then
execute immediate 'update ' || tablenew || ' set accli = ' || rec.acode || ', clcode= ' || rec.clcode || ''; --данные из таблы general_table обновляются в таблице tablename
end if;
end loop;
end;


It turns out that, in the tablenew table, all fields are updated with the same field from the cycle. What is the best way to write in the code so that all fields in the table tablenew table are updated from the general_table table? Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rPman, 2021-06-01
@Raushka

add a condition to the update request where ' || tablenew || '.id=' || tablename.id || 'if id is numeric there (if string or binary, then think about how to correctly pass values ​​through variables or how to escape dangerous characters)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question