I'm inside a cursor that has 10 rows of result.
2 rows perpendicular to a number. example: 1,2 = 1, 3,4 = 2, 5,6 = 6, 7,8 = 9 , 9,10 = 3.
How do I add the values that match the same number inside the cursor?
This is my FETCH
of the cursor, I have 10 rows inside it, the spv_total_item is of a specified spv_moeda_id and I want to add when the spv_moeda_id are the same, how do I do this?
FETCH cursor_comissao INTO SPV_COMISSAO_ID, SPV_COMPRA_ID, SPV_PERCENTUAL, SPV_MOEDA_ID, SPV_DATA_CAMBIO, SPV_TOTAL_ITEM;
I would be very grateful if someone helped me with this logic.
I tried, but the first time I run the row, the spv_moeda_check
is null, and only at the end of the loop will it have a value.
OPEN cursor_comissao;
-- INICIANDO A VARIAVEL DURAÇÃO DO LOOP PARA 0;
SET SPV_DURACAO = 0;
loop_comissoes: LOOP
FETCH cursor_comissao INTO SPV_COMISSAO_ID, SPV_COMPRA_ID, SPV_PERCENTUAL, SPV_MOEDA_ID, SPV_DATA_CAMBIO, SPV_TOTAL_ITEM;
IF CURSOR_FIM = 1 THEN
LEAVE loop_comissoes;
END IF;
IF SPV_DURACAO = SPV_LOOP THEN
SET CURSOR_FIM = 1;
LEAVE loop_comissoes;
END IF;
IF SPV_COMISSAO_ID = SPV_ID_CHECK THEN
SPV_TOTAL_ITEM = SPV_TOTAL_ITEM + SPV_TOTAL_ITEM;
ITERATE loop_comissoes;
END IF;
CALL SP_CONVERTE_REAIS_DATA(SPV_TOTAL_ITEM,SPV_MOEDA_ID,SPV_DATA_CAMBIO,SPV_TOTAL_ITEM);
-- SETANDO O CAMPO VALOR DA ROW ATUAL DO LOOP;
UPDATE cliente_compra_comissao SET valor = SPV_TOTAL_ITEM WHERE id = SPV_COMISSAO_ID;
SET SPV_DURACAO = SPV_DURACAO + 1;
SET SPV_TOTAL_ITEM = 0;
SET SPV_ID_CHECK = SPV_COMISSAO_ID;
END LOOP loop_comissoes;
CLOSE cursor_comissao;
CLOSE cursor_comissao_id;