ALTER TABLE TBMODELO_SIMBOLOGIA_COMP
    ADD IGNORAR INTEGER DEFAULT 0 NOT NULL;
	
/*************************************************************************/
/*************************************************************************/

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_simbologia_comp_b01iu for tbmodelo_simbologia_comp
active before insert or update position 0
AS
DECLARE VARIABLE ETQ_COMP INTEGER;
BEGIN

    :ETQ_COMP = COALESCE((SELECT FIRST 1 A.ID FROM TBMODELO_SIMBOLOGIA_COMP A WHERE A.PECA = NEW.PECA AND A.MODELO_ID = NEW.MODELO_ID), 0);
    
    IF (INSERTING) THEN
    BEGIN

        IF (:ETQ_COMP > 0) THEN
        BEGIN
            EXCEPTION "..." 'A SIMBOLOGIA '||FN_LPAD(:ETQ_COMP, 4, '0')||' JA ESTA CADASTRADA NA PECA '||NEW.PECA;
        END

    END ELSE
    IF (UPDATING) THEN
    BEGIN

        IF (OLD.PECA IS DISTINCT FROM NEW.PECA) THEN
        BEGIN

            IF (:ETQ_COMP > 0 AND NEW.IGNORAR <> 1) THEN
            BEGIN
                EXCEPTION "..." 'A SIMBOLOGIA '||FN_LPAD(:ETQ_COMP, 4, '0')||' JA ESTA CADASTRADA NA PECA '||NEW.PECA;
            END

        END

    END

END
^

SET TERM ; ^

