CREATE TABLE TBMICROREGIAO (
    ID INTEGER NOT NULL,
    DESCRICAO VARCHAR(80),
    UF VARCHAR(10),
    CIDADE VARCHAR(40),
    BAIRRO VARCHAR(40),
    DESCRICAO_RESUMIDA VARCHAR(50));

ALTER TABLE TBMICROREGIAO
ADD CONSTRAINT PK_TBMICROREGIAO
PRIMARY KEY (ID);

CREATE SEQUENCE GTBMICROREGIAO;

SET TERM ^ ;

create trigger tbmicroregiao_bi for tbmicroregiao
active before insert position 0
as
begin
  if (new.id is null) then
    new.id = gen_id(gtbmicroregiao,1);
end
^

SET TERM ; ^

SET TERM ^ ;

CREATE trigger ttbmicroregiao_b01i for tbmicroregiao
active before insert position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' REGISTRO INCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMICROREGIAO',NEW.ID, HISTORICO);
    END
END
^

SET TERM ; ^


SET TERM ^ ;

CREATE trigger ttbmicroregiao_b01u for tbmicroregiao
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( NEW.DESCRICAO <> OLD.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' DESC:'||OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = ' REG.ALTERADO: '||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBMICROREGIAO',NEW.ID, HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE trigger ttbmicroregiao_b01d for tbmicroregiao
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' REGISTRO EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMICROREGIAO',OLD.ID, HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE trigger ttbmicroregiao_b02d for tbmicroregiao
active before delete position 0
AS
BEGIN
   IF(OLD.ID > 0) THEN
   BEGIN
      DELETE FROM TBMICROREGIAO_CIDADE WHERE MICROREGIAO_ID = OLD.ID;
   END
END
^

SET TERM ; ^








