CREATE TABLE TBROYALTIES_CLIENTE (
    ID INTEGER,
    ROYALTIES_ID INTEGER,
    CLIENTE_ID INTEGER);

CREATE INDEX ITBROYALTIES_CLIENTE_ID01
ON TBROYALTIES_CLIENTE (ID);

CREATE INDEX ITBROYALTIES_CLIENTE_ID02
ON TBROYALTIES_CLIENTE (ROYALTIES_ID);

CREATE INDEX ITBROYALTIES_CLIENTE_ID03
ON TBROYALTIES_CLIENTE (CLIENTE_ID);

CREATE SEQUENCE GTBROYALTIES_CLIENTE;

SET TERM ^ ;

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

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbroyalties_cliente_b02i for tbroyalties_cliente
active before insert position 0
AS
declare variable HISTORICO VARCHAR(5000);
begin

    HISTORICO = '';

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

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

end
^

SET TERM ; ^

SET TERM ^ ;

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

    HISTORICO = '';

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

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

end
^

SET TERM ; ^

