CREATE OR ALTER trigger tbcliente_meta_b01iu for tbcliente_meta
active before insert or update position 2
AS
declare variable ID INTEGER;
begin

    for
        SELECT
            ID
        FROM TBCLIENTE_META T
        WHERE ((t.DATA_INICIAL BETWEEN NEW.DATA_INICIAL AND NEW.DATA_FINAL) or (T.DATA_FINAL BETWEEN NEW.DATA_INICIAL AND NEW.DATA_FINAL))
        AND ID <> NEW.ID
        and t.cliente_id = new.cliente_id
        AND T.TIPO = '2'
    into
    :ID
    do begin
        exception "..." 'META JA CADASTRADA NO PERIODO INFORMADO!';
    end

    IF(NEW.TIPO = '1') THEN
    BEGIN
    for
        SELECT
            ID
        FROM TBCLIENTE_META T
        WHERE ID <> NEW.ID
        and t.cliente_id = new.cliente_id
        AND T.TIPO = '1'
    
        into
            :ID
    
        do begin
            exception "..." 'PRIMEIRA COMPRA JA CADASTRADA PARA ESSE CLIENTE!';
        end
    END

end;