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?