SET TERM ^ ;

CREATE trigger ttbpdv_cfe_b10i for tbpdv_cfe
active before insert position 4
AS
DECLARE VARIABLE CHAVE_ACESSO_EXISTENTE INTEGER;
BEGIN

    :CHAVE_ACESSO_EXISTENTE = COALESCE((SELECT FIRST 1 T.ID FROM TBPDV_CFE T WHERE T.CHAVE_ACESSO = NEW.CHAVE_ACESSO AND T.OFFLINE_ID > 0 AND T.ID <> NEW.ID), 0);

    IF(COALESCE(:CHAVE_ACESSO_EXISTENTE, 0) > 0) THEN
    BEGIN
        EXCEPTION "..."'DOCUMENTO FISCAL '||:CHAVE_ACESSO_EXISTENTE||' JA IMPORTADO NO SISTEMA';
    END
        
END
^

SET TERM ; ^

