Exemplo de cursor no Oracle:
create or replace function sup_obter_media_consumo_962
( xItem in char
) return number is
xSaida Number;
xMedia Number;
xAno Number;
xMes Number;
xMaxAnoMes char(6);
xCont Number;
xTotal Number;
begin
SELECT Max(ano_hist_cons||mes_hist_cons) into xMaxAnoMes FROM hist_cons;
xAno:= to_number(SubStr(xMaxAnoMes,1,4));
xMes:= to_number(SubStr(xMaxAnoMes,5,2));
xSaida:= 0;
xMedia:=0;
xTotal:=0;
xCont:=1;
While xCont <= 6 LOOP
Begin
SELECT qtd_consum_real into xSaida FROM hist_cons WHERE cod_item=xItem and ano_hist_cons=xAno and mes_hist_cons=xMes;
exception
when no_data_found then
xSaida:= 0;
end;
xTotal := xTotal + xSaida;
xMes := xMes - 1;
xCont := xCont + 1;
IF xMes = 0 THEN
xMes := 12;
xAno := xAno - 1;
END IF;
END LOOP;
if xTotal > 0 Then
xMedia:= round(xTotal / 6, 3);
End If;
RETURN xMedia;
end sup_obter_media_consumo_962;
Nenhum comentário:
Postar um comentário