R
R
Raushka2021-06-02 21:53:38
Oracle
Raushka, 2021-06-02 21:53:38

How can I exit the loop once all rows in the table have been updated using dynamic sql?

I ask you to help in this situation, namely, how can I exit the loop or stop the loop as soon as all the rows in the CoolTable table have been updated?
In general, there is a prc_uptable procedure, where a table (CoolTable) is supplied as input, then rows from the general_table table loop are updated in this table, and since only three rows are updated according to the condition in the CoolTable table, and there are more than 20 rows in the table from the general_table loop, then the loop continues to work and update the data, so I ask you to help me write a condition for exiting the loop in the code when updating all rows in the CoolTable table, i.e. as soon as all the rows in the CoolTable table are updated, you need to immediately exit the loop or stop it.

create or reolace prc_uptable (tablename varchar2) id --- '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 ='CoolTable' then
  if rec.acode = '159' and rec.clcode  = 'BYV' then
    execute immediate 'update general_table set forsign = 1 where type_cli = '''|| rec.clcode || '''';
      execute immediate 'update ' || tablenew || ' set accli = ' || rec.acode || ', clcode= ' || rec.clcode || ''; --данные из таблы general_table обновляются в таблице tablename
  elsif rec.acode = '357' and rec.clcode  = 'MYV' then
    execute immediate 'update general_table set forsign = 1 where type_cli = '''|| rec.clcode || '''';
      execute immediate 'update ' || tablenew || ' set accli = ' || rec.acode || ', clcode= ' || rec.clcode || ''; 
   elsif rec.acode = '789' and rec.clcode  = 'BYV' then
    execute immediate 'update general_table set forsign = 1 where type_cli = '''|| rec.clcode || '''';
      execute immediate 'update ' || tablenew || ' set accli = ' || rec.acode || ', clcode= ' || rec.clcode || ''; 
   end if;
end if;
end loop;
end;


can prescribe type exit when tablenew and I will not think further. Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
idShura, 2021-06-02
@Raushka

Yes you can, pl-sql-exit-loop

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question