CREATE TABLE TBROYALTIES_MODELO (
    ID INTEGER NOT NULL,
    ROYALTIES_ID INTEGER,
    MODELO_ID INTEGER,
    PERCENTUAL NUMERIC(15,2));

ALTER TABLE TBROYALTIES_MODELO
ADD CONSTRAINT PK_TBROYALTIES_MODELO
PRIMARY KEY (ID);

CREATE INDEX ITBROYALTIES_MODELO_ID01
ON TBROYALTIES_MODELO (ROYALTIES_ID);

CREATE INDEX ITBROYALTIES_MODELO_ID02
ON TBROYALTIES_MODELO (MODELO_ID);

CREATE SEQUENCE GTBROYALTIES_MODELO;

SET TERM ^ ;

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

SET TERM ; ^

SET TERM ^ ;

CREATE trigger ttbroyalties_modelo_b02i for tbroyalties_modelo
active before insert position 0
AS
declare variable HISTORICO VARCHAR(5000);
begin

    HISTORICO = '';

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

    EXECUTE PROCEDURE SPI_HISTORICO('TBROYALTIES', NEW.ROYALTIES_ID, HISTORICO);

end
^

SET TERM ; ^

SET TERM ^ ;

CREATE trigger ttbroyalties_modelo_b01d for tbroyalties_modelo
active before delete position 0
AS
declare variable HISTORICO VARCHAR(5000);
begin

    HISTORICO = '';

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

    EXECUTE PROCEDURE SPI_HISTORICO('TBROYALTIES', OLD.ROYALTIES_ID, HISTORICO);

end
^

SET TERM ; ^







