SET TERM ^ ;

CREATE OR ALTER procedure SPC_CHECK_PDV_CFE
returns (
    PDV_CFE_ID_EXC integer)
as
declare variable MIN_PDV_CFE_ID integer;
declare variable MAX_PDV_CFE_ID integer;
declare variable RET_ID integer;
declare variable CHECK_PDV_CFE_ID integer;
BEGIN
    :MIN_PDV_CFE_ID = COALESCE((SELECT MIN(T.ID) FROM TBPDV_CFE T WHERE T.DATA_EMISSAO = CURRENT_DATE), 0);
    :MAX_PDV_CFE_ID = COALESCE((SELECT MAX(T.ID) FROM TBPDV_CFE T WHERE T.DATA_EMISSAO = CURRENT_DATE), 0);

    FOR SELECT
            K.OUTPUTVALUE
    FROM GENERATE_RANGE(:MIN_PDV_CFE_ID, :MAX_PDV_CFE_ID) K
    INTO :RET_ID
    DO BEGIN

        :CHECK_PDV_CFE_ID = COALESCE((SELECT FIRST 1 T.ID FROM TBPDV_CFE T WHERE T.ID = :RET_ID), 0);

        IF(:CHECK_PDV_CFE_ID = 0) THEN
        BEGIN
            :PDV_CFE_ID_EXC = :RET_ID;
            SUSPEND;
        END


    END


        
END
^

SET TERM ; ^

