Loop inside SQL loop

Asked

Viewed 421 times

0

I have the following code in a procedure in the mysql:

BEGIN

declare v_unidade varchar(10);
declare v_conta varchar(256);
declare v_cnt INT;
DECLARE done INT DEFAULT 0;

declare c_unidade cursor for 
        SELECT nu_unidade 
        FROM tb_unidades
        order by nu_unidade;

declare c_conta cursor for 
        SELECT conta
        FROM tb_contas where not trim(conta)=''
        order by conta;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;


open c_unidade;
loop1: loop
    fetch c_unidade into v_unidade;
    IF done = 1 THEN
        LEAVE loop1;
    END IF;
    open c_conta;
    loop2: loop
        fetch c_conta into v_conta;
        IF done = 1 THEN
            LEAVE loop2;
        END IF;
        select count(*) into v_cnt from tb_temporaria where unidade = v_unidade and conta = v_conta and ano_mes = v_ano_mes;
        if v_cnt=0 then
            INSERT INTO tb_temporaria (descricao, conta, valor, unidade, ano_mes)
            SELECT descricao, conta, 0, v_unidade, v_ano_mes FROM tb_contas where conta = v_conta;
        end if;
    end loop;
end loop;

END

But the loops do not repeat themselves, running only for one unit. It passes only once and closes the procedure.

Does anyone know how to adjust this? I understood the logic of DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1, would be like "Zera it" again?

1 answer

1


I found the answer on this link: https://stackoverflow.com/questions/6099500/multiple-cursors-in-nested-loops-in-mysql.

The final version looks like this:

BEGIN

BLOCK1: begin
    declare v_unidade varchar(3);                    
    DECLARE done boolean DEFAULT FALSE; 

        declare c_unidade cursor for 
            SELECT nu_unidade 
            FROM tb_unidades
            order by nu_unidade;
    declare continue handler for not found  
        set done = TRUE;           

        open c_unidade;
    LOOP1: loop
        fetch c_unidade into v_unidade;
        if done then
            close c_unidade;
            leave LOOP1;
        end if;
        BLOCK2: begin
            declare v_conta varchar(256);
            declare v_descricao varchar(256);
            declare v_cnt INT;
            DECLARE done2 boolean DEFAULT FALSE;

            declare c_conta cursor for 
                            SELECT conta, descricao
                            FROM tb_contas where not trim(conta)=''
                            order by conta;
           declare continue handler for not found  
                            set done2 = TRUE; 

            open c_conta;
            LOOP2: loop
                fetch c_conta into v_conta, v_descricao;
                if done2 then
                    close c_conta;
                    leave LOOP2;
                end if;

                select count(*) into v_cnt from tb_temporaria
                where upper(conta) = upper(v_conta) and upper(descricao) = upper(v_descricao) and ano_mes = v_ano_mes and unidade = v_unidade;

                if v_cnt=0 then
                    INSERT INTO tb_temporaria (descricao, conta, valor, unidade, ano_mes)
                    SELECT descricao, conta, 0, v_unidade, v_ano_mes
                    FROM tb_contas where conta = v_conta and descricao = v_descricao;
                end if;
            end loop LOOP2;
        end BLOCK2;
    end loop LOOP1;
end BLOCK1;
END

Browser other questions tagged

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