DROP INDEX ITBGP_PERFIL_SKU_01;
DROP INDEX ITBGP_PERFIL_SKU_02;

CREATE OR ALTER trigger ttbgp_perfil_sku_b9u for tbgp_perfil_sku
active before update position 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
begin

end;

ALTER TABLE TBGP_PERFIL_SKU ADD OLD_PERFIL VARCHAR(10) DEFAULT '';
update TBGP_PERFIL_SKU set OLD_PERFIL = PERFIL;
ALTER TABLE TBGP_PERFIL_SKU DROP PERFIL;
ALTER TABLE TBGP_PERFIL_SKU ADD PERFIL VARCHAR(10) DEFAULT '';
update TBGP_PERFIL_SKU set PERFIL = OLD_PERFIL;
ALTER TABLE TBGP_PERFIL_SKU DROP OLD_PERFIL;

CREATE INDEX ITBGP_PERFIL_SKU_01 ON TBGP_PERFIL_SKU (GP_ID,PERFIL);
CREATE INDEX ITBGP_PERFIL_SKU_02 ON TBGP_PERFIL_SKU (PERFIL);

CREATE OR ALTER trigger ttbgp_perfil_sku_b9u for tbgp_perfil_sku
active before update position 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
begin

   HISTORICO = '';

   if (OLD.MULTIPLICADOR <> NEW.MULTIPLICADOR) then
       BEGIN
       HISTORICO = HISTORICO||'Perfil '||OLD.PERFIL||' Mult.'||OLD.MULTIPLICADOR||'->'||NEW.MULTIPLICADOR;
       END

   if (HISTORICO <> '') Then
       Begin
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBESTEIRA', OLD.GP_ID,
      (Select First 1 MON$USER from mon$attachments where mon$attachment_id = current_connection),
      (Select First 1 MON$REMOTE_ADDRESS from mon$attachments where mon$attachment_id = current_connection),
       :Historico);
       End
End;

