CREATE OR ALTER trigger tbperfil_b01u for tbperfil
active before update position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.DESCRICAO <> NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||'DESC:'|| OLD.DESCRICAO||'->'||NEW.DESCRICAO FROM 1 FOR 500));
    END

    IF ( OLD.CAP_PRODUTIVA <> NEW.CAP_PRODUTIVA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' CAP PROD:'|| OLD.CAP_PRODUTIVA||'->'||NEW.CAP_PRODUTIVA FROM 1 FOR 500));
    END

    IF ( OLD.TEMPO_FADIGA <> NEW.TEMPO_FADIGA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' FADIGA:'|| OLD.TEMPO_FADIGA||'%'||'->'||NEW.TEMPO_FADIGA||'%' FROM 1 FOR 500));
    END

    IF ( OLD.LOCALIZACAO_ID <> NEW.LOCALIZACAO_ID) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' LOC:'|| OLD.LOCALIZACAO_ID||'->'||NEW.LOCALIZACAO_ID FROM 1 FOR 500));
    END

    IF ( OLD.TRATAMENTO_PEDIDO_MP <> NEW.TRATAMENTO_PEDIDO_MP) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TRAT. PED.  '|| OLD.TRATAMENTO_PEDIDO_MP||'->'||NEW.TRATAMENTO_PEDIDO_MP FROM 1 FOR 500));
    END

    IF ( OLD.DESCRICAO_COMPLETA <> NEW.DESCRICAO_COMPLETA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DESC.COM:'|| OLD.DESCRICAO_COMPLETA||'->'||NEW.DESCRICAO_COMPLETA FROM 1 FOR 500));
    END

    IF ( OLD.QTD_MIN <> NEW.QTD_MIN) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QTD.MIN:'|| OLD.QTD_MIN||'->'||NEW.QTD_MIN FROM 1 FOR 500));
    END

    IF ( OLD.QTD_MULT <> NEW.QTD_MULT) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QTD.MULT:'|| OLD.QTD_MULT||'->'||NEW.QTD_MULT FROM 1 FOR 500));
    END

    IF ( OLD.AGRUPAMENTO <> NEW.AGRUPAMENTO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' AGRUP. '|| OLD.AGRUPAMENTO||'->'||NEW.AGRUPAMENTO FROM 1 FOR 500));
    END

    IF ( OLD.EFIC_MINIMA <> NEW.EFIC_MINIMA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' EFIO. MIN. '|| OLD.EFIC_MINIMA||'->'||NEW.EFIC_MINIMA FROM 1 FOR 500));
    END

    IF ( OLD.PONTO_REPOSICAO <> NEW.PONTO_REPOSICAO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' PON. REP. '|| OLD.PONTO_REPOSICAO||'->'||NEW.PONTO_REPOSICAO FROM 1 FOR 500));
    END

    IF ( OLD.FAMILIA_ID <> NEW.FAMILIA_ID) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' FAMILIA: '|| OLD.FAMILIA_ID||'->'||NEW.FAMILIA_ID FROM 1 FOR 500));
    END

    IF ( OLD.COR_DIFERENCIADA <> NEW.COR_DIFERENCIADA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' COR. DIF. '|| OLD.COR_DIFERENCIADA||'->'||NEW.COR_DIFERENCIADA FROM 1 FOR 500));
    END

    IF ( OLD.COR_SOBENCOMENDA <> NEW.COR_SOBENCOMENDA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' COR. SOB ENC. '|| OLD.COR_SOBENCOMENDA||'->'||NEW.COR_SOBENCOMENDA FROM 1 FOR 500));
    END

    IF ( OLD.QTD_MIN_SOBENC <> NEW.QTD_MIN_SOBENC) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QTD MIN SOB. '|| OLD.QTD_MIN_SOBENC||'->'||NEW.QTD_MIN_SOBENC FROM 1 FOR 500));
    END

    IF ( OLD.QTD_MULT_SOBENC <> NEW.QTD_MULT_SOBENC) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QTD MULT SOB. '|| OLD.QTD_MULT_SOBENC||'->'||NEW.QTD_MULT_SOBENC FROM 1 FOR 500));
    END

    IF ( OLD.PZO1_PED_D1 <> NEW.PZO1_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM NORMAL PROD. '|| OLD.PZO1_PED_D1||'->'||NEW.PZO1_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZO1_PED_D2 <> NEW.PZO1_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG NORMAL PROD. '|| OLD.PZO1_PED_D2||'->'||NEW.PZO1_PED_D2 FROM 1 FOR 500));
    END

     IF ( OLD.PZO1_PED_D3 <> NEW.PZO1_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER NORMAL PROD. '|| OLD.PZO1_PED_D3||'->'||NEW.PZO1_PED_D3 FROM 1 FOR 500));
    END

     IF ( OLD.PZO1_PED_D4 <> NEW.PZO1_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA NORMAL PROD. '|| OLD.PZO1_PED_D4||'->'||NEW.PZO1_PED_D4 FROM 1 FOR 500));
    END

     IF ( OLD.PZO1_PED_D5 <> NEW.PZO1_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI NORMAL PROD. '|| OLD.PZO1_PED_D5||'->'||NEW.PZO1_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZO1_PED_D6 <> NEW.PZO1_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX NORMAL PROD. '|| OLD.PZO1_PED_D6||'->'||NEW.PZO1_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZO1_PED_D7 <> NEW.PZO1_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB NORMAL PROD. '|| OLD.PZO1_PED_D7||'->'||NEW.PZO1_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.PZO2_PED_D1 <> NEW.PZO2_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM NORMAL CLI. '|| OLD.PZO2_PED_D1||'->'||NEW.PZO2_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZO2_PED_D2 <> NEW.PZO2_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG NORMAL CLI. '|| OLD.PZO2_PED_D2||'->'||NEW.PZO2_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZO2_PED_D3 <> NEW.PZO2_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER NORMAL CLI. '|| OLD.PZO2_PED_D3||'->'||NEW.PZO2_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZO2_PED_D4 <> NEW.PZO2_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA NORMAL CLI. '|| OLD.PZO2_PED_D4||'->'||NEW.PZO2_PED_D4 FROM 1 FOR 500));
    END

    IF ( OLD.PZO2_PED_D5 <> NEW.PZO2_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI NORMAL CLI. '|| OLD.PZO2_PED_D5||'->'||NEW.PZO2_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZO2_PED_D6 <> NEW.PZO2_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX NORMAL CLI. '|| OLD.PZO2_PED_D6||'->'||NEW.PZO2_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZO2_PED_D7 <> NEW.PZO2_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB NORMAL CLI. '|| OLD.PZO2_PED_D7||'->'||NEW.PZO2_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD1_PED_D1 <> NEW.PZOD1_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM COR DIF PROD. '|| OLD.PZOD1_PED_D1||'->'||NEW.PZOD1_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD1_PED_D2 <> NEW.PZOD1_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG COR DIF PROD. '|| OLD.PZOD1_PED_D2||'->'||NEW.PZOD1_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD1_PED_D3 <> NEW.PZOD1_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER COR DIF PROD. '|| OLD.PZOD1_PED_D3||'->'||NEW.PZOD1_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD1_PED_D4 <> NEW.PZOD1_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA COR DIF PROD. '|| OLD.PZOD1_PED_D4||'->'||NEW.PZOD1_PED_D4 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD1_PED_D5 <> NEW.PZOD1_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI COR DIF PROD. '|| OLD.PZOD1_PED_D5||'->'||NEW.PZOD1_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD1_PED_D6 <> NEW.PZOD1_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX COR DIF PROD. '|| OLD.PZOD1_PED_D6||'->'||NEW.PZOD1_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD1_PED_D7 <> NEW.PZOD1_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB COR DIF PROD. '|| OLD.PZOD1_PED_D7||'->'||NEW.PZOD1_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD2_PED_D1 <> NEW.PZOD2_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM COR DIF CLI. '|| OLD.PZOD2_PED_D1||'->'||NEW.PZOD2_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD2_PED_D2 <> NEW.PZOD2_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG COR DIF CLI. '|| OLD.PZOD2_PED_D2||'->'||NEW.PZOD2_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD2_PED_D3 <> NEW.PZOD2_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER COR DIF CLI. '|| OLD.PZOD2_PED_D3||'->'||NEW.PZOD2_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD2_PED_D4 <> NEW.PZOD2_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA COR DIF CLI. '|| OLD.PZOD2_PED_D4||'->'||NEW.PZOD2_PED_D4 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD2_PED_D5 <> NEW.PZOD2_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI COR DIF CLI. '|| OLD.PZOD2_PED_D5||'->'||NEW.PZOD2_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD2_PED_D6 <> NEW.PZOD2_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX COR DIF CLI. '|| OLD.PZOD2_PED_D6||'->'||NEW.PZOD2_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD2_PED_D7 <> NEW.PZOD2_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB COR DIF CLI. '|| OLD.PZOD2_PED_D7||'->'||NEW.PZOD2_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS1_PED_D1 <> NEW.PZOS1_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM SOB-ENC PROD. '|| OLD.PZOS1_PED_D1||'->'||NEW.PZOS1_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS1_PED_D2 <> NEW.PZOS1_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG SOB-ENC PROD. '|| OLD.PZOS1_PED_D2||'->'||NEW.PZOS1_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS1_PED_D3 <> NEW.PZOS1_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER SOB-ENC PROD. '|| OLD.PZOS1_PED_D3||'->'||NEW.PZOS1_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS1_PED_D4 <> NEW.PZOS1_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA SOB-ENC PROD. '|| OLD.PZOS1_PED_D4||'->'||NEW.PZOS1_PED_D4 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS1_PED_D5 <> NEW.PZOS1_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI SOB-ENC PROD. '|| OLD.PZOS1_PED_D5||'->'||NEW.PZOS1_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS1_PED_D6 <> NEW.PZOS1_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX SOB-ENC PROD. '|| OLD.PZOS1_PED_D6||'->'||NEW.PZOS1_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS1_PED_D7 <> NEW.PZOS1_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB SOB-ENC PROD. '|| OLD.PZOS1_PED_D7||'->'||NEW.PZOS1_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS2_PED_D1 <> NEW.PZOS2_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM SOB-ENC CLI. '|| OLD.PZOS2_PED_D1||'->'||NEW.PZOS2_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS2_PED_D2 <> NEW.PZOS2_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG SOB-ENC CLI. '|| OLD.PZOS2_PED_D2||'->'||NEW.PZOS2_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS2_PED_D3 <> NEW.PZOS2_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER SOB-ENC CLI. '|| OLD.PZOS2_PED_D3||'->'||NEW.PZOS2_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS2_PED_D4 <> NEW.PZOS2_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA SOB-ENC CLI. '|| OLD.PZOS2_PED_D3||'->'||NEW.PZOS2_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS2_PED_D5 <> NEW.PZOS2_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI SOB-ENC CLI. '|| OLD.PZOS2_PED_D5||'->'||NEW.PZOS2_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS2_PED_D6 <> NEW.PZOS2_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX SOB-ENC CLI. '|| OLD.PZOS2_PED_D6||'->'||NEW.PZOS2_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS2_PED_D7 <> NEW.PZOS2_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB SOB-ENC CLI. '|| OLD.PZOS2_PED_D7||'->'||NEW.PZOS2_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.TRATAMENTO_PEDIDO <> NEW.TRATAMENTO_PEDIDO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TRAT PED. '|| OLD.TRATAMENTO_PEDIDO||'->'||NEW.TRATAMENTO_PEDIDO FROM 1 FOR 500));
    END

    IF ( OLD.AGRUPAMENTO2 <> NEW.AGRUPAMENTO2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' AGRUP2. '|| OLD.AGRUPAMENTO2||'->'||NEW.AGRUPAMENTO2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE1_PED_D1 <> NEW.PZOE1_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM EST MIN PROD. '|| OLD.PZOE1_PED_D1||'->'||NEW.PZOE1_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE1_PED_D2 <> NEW.PZOE1_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG EST MIN PROD. '|| OLD.PZOE1_PED_D2||'->'||NEW.PZOE1_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE1_PED_D3 <> NEW.PZOE1_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER EST MIN PROD. '|| OLD.PZOE1_PED_D3||'->'||NEW.PZOE1_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE1_PED_D4 <> NEW.PZOE1_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA EST MIN PROD. '|| OLD.PZOE1_PED_D4||'->'||NEW.PZOE1_PED_D4 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE1_PED_D5 <> NEW.PZOE1_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI EST MIN PROD. '|| OLD.PZOE1_PED_D5||'->'||NEW.PZOE1_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE1_PED_D6 <> NEW.PZOE1_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX EST MIN PROD. '|| OLD.PZOE1_PED_D6||'->'||NEW.PZOE1_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE1_PED_D7 <> NEW.PZOE1_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB EST MIN PROD. '|| OLD.PZOE1_PED_D7||'->'||NEW.PZOE1_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE2_PED_D1 <> NEW.PZOE2_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM EST MIN CLI. '|| OLD.PZOE2_PED_D1||'->'||NEW.PZOE2_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE2_PED_D2 <> NEW.PZOE2_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG EST MIN CLI. '|| OLD.PZOE2_PED_D2||'->'||NEW.PZOE2_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE2_PED_D3 <> NEW.PZOE2_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER EST MIN CLI. '|| OLD.PZOE2_PED_D3||'->'||NEW.PZOE2_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE2_PED_D4 <> NEW.PZOE2_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA EST MIN CLI. '|| OLD.PZOE2_PED_D4||'->'||NEW.PZOE2_PED_D4 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE2_PED_D5 <> NEW.PZOE2_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI EST MIN CLI. '|| OLD.PZOE2_PED_D5||'->'||NEW.PZOE2_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE2_PED_D6 <> NEW.PZOE2_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX EST MIN CLI. '|| OLD.PZOE2_PED_D6||'->'||NEW.PZOE2_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE2_PED_D7 <> NEW.PZOE2_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB EST MIN CLI. '|| OLD.PZOE2_PED_D7||'->'||NEW.PZOE2_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.EFIC_A <> NEW.EFIC_A) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' EFIC A. '|| OLD.EFIC_A||'->'||NEW.EFIC_A FROM 1 FOR 500));
    END

    IF ( OLD.EFIC_B <> NEW.EFIC_B) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' EFIC B. '|| OLD.EFIC_B||'->'||NEW.EFIC_B FROM 1 FOR 500));
    END

    IF ( OLD.PERDA_A <> NEW.PERDA_A) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' PERDA A. '|| OLD.PERDA_A||'->'||NEW.PERDA_A FROM 1 FOR 500));
    END

    IF ( OLD.PERDA_B <> NEW.PERDA_B) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' PERDA A. '|| OLD.PERDA_B||'->'||NEW.PERDA_B FROM 1 FOR 500));
    END

    IF ( OLD.ENDERECAMENTO_SOBRA <> NEW.ENDERECAMENTO_SOBRA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' END SOB. '|| OLD.ENDERECAMENTO_SOBRA||'->'||NEW.ENDERECAMENTO_SOBRA FROM 1 FOR 500));
    END

    IF ( OLD.TIPO_REPOSICAO <> NEW.TIPO_REPOSICAO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TIP REP. '|| OLD.TIPO_REPOSICAO||'->'||NEW.TIPO_REPOSICAO FROM 1 FOR 500));
    END

    IF ( OLD.COR_BASICA <> NEW.COR_BASICA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' COR BASICA: '|| OLD.COR_BASICA||'->'||NEW.COR_BASICA FROM 1 FOR 500));
    END

    IF ( OLD.PZOI1_PED_D1 <> NEW.PZOI1_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM EST INDIS PROD. '|| OLD.PZOI1_PED_D1||'->'||NEW.PZOI1_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI1_PED_D2 <> NEW.PZOI1_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG EST INDIS PROD. '|| OLD.PZOI1_PED_D2||'->'||NEW.PZOI1_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI1_PED_D3 <> NEW.PZOI1_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER EST INDIS PROD. '|| OLD.PZOI1_PED_D3||'->'||NEW.PZOI1_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI1_PED_D4 <> NEW.PZOI1_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA EST INDIS PROD. '|| OLD.PZOI1_PED_D4||'->'||NEW.PZOI1_PED_D4 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI1_PED_D5 <> NEW.PZOI1_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI EST INDIS PROD. '|| OLD.PZOI1_PED_D5||'->'||NEW.PZOI1_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI1_PED_D6 <> NEW.PZOI1_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX EST INDIS PROD. '|| OLD.PZOI1_PED_D6||'->'||NEW.PZOI1_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI1_PED_D7 <> NEW.PZOI1_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB EST INDIS PROD. '|| OLD.PZOI1_PED_D7||'->'||NEW.PZOI1_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI2_PED_D1 <> NEW.PZOI2_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM EST INDIS CLI. '|| OLD.PZOI2_PED_D1||'->'||NEW.PZOI2_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI2_PED_D2 <> NEW.PZOI2_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG EST INDIS CLI. '|| OLD.PZOI2_PED_D2||'->'||NEW.PZOI2_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI2_PED_D3 <> NEW.PZOI2_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER EST INDIS CLI. '|| OLD.PZOI2_PED_D3||'->'||NEW.PZOI2_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI2_PED_D4 <> NEW.PZOI2_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA EST INDIS CLI. '|| OLD.PZOI2_PED_D4||'->'||NEW.PZOI2_PED_D4 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI2_PED_D5 <> NEW.PZOI2_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI EST INDIS CLI. '|| OLD.PZOI2_PED_D5||'->'||NEW.PZOI2_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI2_PED_D6 <> NEW.PZOI2_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX EST INDIS CLI. '|| OLD.PZOI2_PED_D6||'->'||NEW.PZOI2_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI2_PED_D6 <> NEW.PZOI2_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX EST INDIS CLI. '|| OLD.PZOI2_PED_D6||'->'||NEW.PZOI2_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI2_PED_D7 <> NEW.PZOI2_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB EST INDIS CLI. '|| OLD.PZOI2_PED_D7||'->'||NEW.PZOI2_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.ALOCAR_MULTIPLO <> NEW.ALOCAR_MULTIPLO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' ALOC MULT. '|| OLD.ALOCAR_MULTIPLO||'->'||NEW.ALOCAR_MULTIPLO FROM 1 FOR 500));
    END

    IF ( OLD.TELA_ADD_PEDIDO <> NEW.TELA_ADD_PEDIDO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TELA ADD PEDIDO. '|| OLD.TELA_ADD_PEDIDO||'->'||NEW.TELA_ADD_PEDIDO FROM 1 FOR 500));
    END

    IF ( OLD.AMOSTRA <> NEW.AMOSTRA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' AMOSTRA: '|| OLD.AMOSTRA||'->'||NEW.AMOSTRA FROM 1 FOR 500));
    END

    IF ( OLD.CONSIGNACAO_FITNESS <> NEW.CONSIGNACAO_FITNESS) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' CONS.FIT: '|| OLD.CONSIGNACAO_FITNESS||'->'||NEW.CONSIGNACAO_FITNESS FROM 1 FOR 500));
    END 

    IF ( OLD.TRANSFERENCIA <> NEW.TRANSFERENCIA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TRANSF: '|| OLD.TRANSFERENCIA||'->'||NEW.TRANSFERENCIA FROM 1 FOR 500));
    END 

    IF ( OLD.ALOCAR_PEDIDO_PENDENTE <> NEW.ALOCAR_PEDIDO_PENDENTE) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' ALOC.PEND: '|| OLD.ALOCAR_PEDIDO_PENDENTE||'->'||NEW.ALOCAR_PEDIDO_PENDENTE FROM 1 FOR 500));
    END 

    IF ( OLD.FAMILIA_PEDIDO <> NEW.FAMILIA_PEDIDO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' FAM PED. '|| OLD.FAMILIA_PEDIDO||'->'||NEW.FAMILIA_PEDIDO FROM 1 FOR 500));
    END

    IF ( OLD.EXIGE_VINCULO_CLIENTE <> NEW.EXIGE_VINCULO_CLIENTE) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' VINC.CLIENTE: '|| OLD.EXIGE_VINCULO_CLIENTE||'->'||NEW.EXIGE_VINCULO_CLIENTE FROM 1 FOR 500));
    END

    IF ( OLD.PERMITE_INCLUIR_CLIENTE <> NEW.PERMITE_INCLUIR_CLIENTE) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' INC.CLIENTE: '|| OLD.PERMITE_INCLUIR_CLIENTE||'->'||NEW.PERMITE_INCLUIR_CLIENTE FROM 1 FOR 500));
    END                   

    IF ( OLD.ANALISE_LIMITE_CREDITO <> NEW.ANALISE_LIMITE_CREDITO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' VERIF.CRED: '|| OLD.ANALISE_LIMITE_CREDITO||'->'||NEW.ANALISE_LIMITE_CREDITO FROM 1 FOR 500));
    END                   

    IF ( OLD.CLIENTE_REP_VINCULO  <> NEW.CLIENTE_REP_VINCULO ) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' VINC.REP: '|| OLD.CLIENTE_REP_VINCULO ||'->'||NEW.CLIENTE_REP_VINCULO  FROM 1 FOR 500));
    END

    IF ( OLD.ESTABELECIMENTO_CODIGO <> NEW.ESTABELECIMENTO_CODIGO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' ESTAB:'|| OLD.ESTABELECIMENTO_CODIGO||'->'||NEW.ESTABELECIMENTO_CODIGO FROM 1 FOR 500));
    END

    IF ( OLD.CFOP_CODIGO <> NEW.CFOP_CODIGO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' CFOP:'|| OLD.CFOP_CODIGO||'->'||NEW.CFOP_CODIGO FROM 1 FOR 500));
    END
    
    IF ( OLD.OPERACAO_CODIGO <> NEW.OPERACAO_CODIGO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' OP.FISCAL:'|| OLD.OPERACAO_CODIGO||'->'||NEW.OPERACAO_CODIGO FROM 1 FOR 500));
    END

    IF ( OLD.TIPO_DANFE <> NEW.TIPO_DANFE) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TP.DANFE:'|| OLD.TIPO_DANFE||'->'||NEW.TIPO_DANFE FROM 1 FOR 500));
    END

    IF ( OLD.TIPO_DANFE <> NEW.TIPO_DANFE) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' IND.PRESENC:'|| OLD.IND_PRESENCA||'->'||NEW.IND_PRESENCA FROM 1 FOR 500));
    END

    IF ( OLD.TIPO_DANFE <> NEW.TIPO_DANFE) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' COMP.CARG.AUT.:'|| OLD.COMP_CARGA_AUTOMATICA||'->'||NEW.COMP_CARGA_AUTOMATICA FROM 1 FOR 500));
    END

    IF ( OLD.HABILITA_CONSUMIDOR_FINAL <> NEW.HABILITA_CONSUMIDOR_FINAL) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' HAB.CONS.FINAL:'|| OLD.HABILITA_CONSUMIDOR_FINAL||'->'||NEW.HABILITA_CONSUMIDOR_FINAL FROM 1 FOR 500));
    END

    IF ( OLD.HABILITA_EAN <> NEW.HABILITA_EAN) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' HAB.EAN:'|| OLD.HABILITA_EAN||'->'||NEW.HABILITA_EAN FROM 1 FOR 500));
    END

    IF ( OLD.HABILITA_PRE_PEDIDO <> NEW.HABILITA_PRE_PEDIDO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' HAB.PRE PED:'|| OLD.HABILITA_PRE_PEDIDO||'->'||NEW.HABILITA_PRE_PEDIDO FROM 1 FOR 500));
    END

    IF ( OLD.HABILITA_PRE_PEDIDO <> NEW.HABILITA_PRE_PEDIDO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' ORDENAC.:'|| OLD.ORDENACAO_ITENS_NF||'->'||NEW.ORDENACAO_ITENS_NF FROM 1 FOR 500));
    END

    IF (OLD.TABELA_PRECO_ID <> NEW.TABELA_PRECO_ID) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TABELA DE PRECO:'|| OLD.TABELA_PRECO_ID||'->'||NEW.TABELA_PRECO_ID FROM 1 FOR 500));
    END

    IF (OLD.PDV <> NEW.PDV) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' HAB.PERFIL PDV:'|| OLD.PDV||'->'||NEW.PDV FROM 1 FOR 500));
    END

    IF (OLD.CAIXA_PDV IS DISTINCT FROM NEW.CAIXA_PDV) THEN
    BEGIN
        HISTORICO = HISTORICO||' CAIXA PDV:'||COALESCE(OLD.CAIXA_PDV, 0)||'->'||COALESCE(NEW.CAIXA_PDV, 0);
    END

    IF (OLD.NF_NATUREZA <> NEW.NF_NATUREZA) THEN
    BEGIN
        HISTORICO = HISTORICO||' NAT.NF:'||OLD.NF_NATUREZA||'->'||NEW.NF_NATUREZA ;
    END

    IF (OLD.NF_SERIE <> NEW.NF_SERIE) THEN
    BEGIN
        HISTORICO = HISTORICO||' SERIE NF:'|| OLD.NF_SERIE||'->'||NEW.NF_SERIE;
    END

    IF (OLD.TIPO_FATURAMENTO_PEDIDO IS DISTINCT FROM NEW.TIPO_FATURAMENTO_PEDIDO) THEN
    BEGIN
        HISTORICO = HISTORICO||' TP.FAT.PED:'|| COALESCE(OLD.TIPO_FATURAMENTO_PEDIDO, '')||'->'||COALESCE(NEW.TIPO_FATURAMENTO_PEDIDO, '');
    END

    IF (OLD.HABILITA_SPED_NF IS DISTINCT FROM NEW.HABILITA_SPED_NF) THEN
    BEGIN
        HISTORICO = HISTORICO||' HAB.SPED NF:'|| COALESCE(OLD.HABILITA_SPED_NF, 0)||'->'||COALESCE(NEW.HABILITA_SPED_NF, 0);
    END
    
    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPERFIL', OLD.CONTROLE, HISTORICO);
    END
END;