Loop, For, PL/SQL cursors (Doubt)

Asked

Viewed 423 times

1

Good morning Guys, my doubt is the following, my table is so currently:

Nome    Idade   ID
João    5       
Lucas   5   
João    2   
Lucas   1   

I wish she’d stay that way:

Nome    Idade   ID
João    2       1
João    5       2
Lucas   1       1
Lucas   5       2

Only my cursor is leaving the table like this:

Nome    Idade   ID
João    2       1
João    5       1
Lucas   1       2
Lucas   5       2

someone can help me, this is my cursor:

Declare
  v_contador  number := 0;
  v_min_idade number := 0;

  cursor c_i is
    select t.*, rowid from teste_matheus t order by nome, idade asc;

begin
  for i in c_i loop

    select min(idade)
      into v_min_idade
      from teste_matheus
     where id is null
       and nome = i.nome;

    if i.idade = v_min_idade and i.id is null then
       v_contador := v_contador + 1; 

      update teste_matheus t
         set id = v_contador
       WHERE id is null
         and nome = i.nome;

     END IF;
    END LOOP;
   END;

Thank you! ^^

  • GOT!!! In case someone goes through the same trouble, is there. Declare V_ant varchar2(100) := 'X'; v_counter number:= 0; v_min_age number := 0; cursor c_i is select t.*, rowid from teste_matheus t order by name, age asc; Begin for i in c_i loop if v_ant <> i.nome then&#xA; v_contador := 1;&#xA; v_ant := i.nome;&#xA; else&#xA; v_contador := v_contador + 1;&#xA; end if;&#xA; &#xA; update teste_matheus t set id = v_contador WHERE t.rowid = i.rowid;&#xA; commit;&#xA; END LOOP;&#xA;END;

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.