ALTER TABLE TBGRADE
    ADD ETQ01 VARCHAR(20),
    ADD ETQ02 VARCHAR(20),
    ADD ETQ03 VARCHAR(20),
    ADD ETQ04 VARCHAR(20),
    ADD ETQ05 VARCHAR(20),
    ADD ETQ06 VARCHAR(20),
    ADD ETQ07 VARCHAR(20),
    ADD ETQ08 VARCHAR(20),
    ADD ETQ09 VARCHAR(20),
    ADD ETQ10 VARCHAR(20),
    ADD ETQ11 VARCHAR(20),
    ADD ETQ12 VARCHAR(20),
    ADD ETQ13 VARCHAR(20),
    ADD ETQ14 VARCHAR(20),
    ADD ETQ15 VARCHAR(20),
    ADD ETQ16 VARCHAR(20),
    ADD ETQ17 VARCHAR(20),
    ADD ETQ18 VARCHAR(20),
    ADD ETQ19 VARCHAR(20),
    ADD ETQ20 VARCHAR(20);

/**********************************************************************/
/**********************************************************************/

SET TERM ^ ;

CREATE OR ALTER trigger ttbgrade_b02u for tbgrade
active before update position 0
AS

DECLARE VARIABLE HISTORICO VARCHAR(500);

BEGIN

    HISTORICO = '';

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


    IF ( OLD.STATUS <> NEW.STATUS) THEN
    BEGIN
       if (NEW.STATUS = '0') then
           HISTORICO = HISTORICO||' Status Inativado.';
       if (NEW.STATUS = '1') then
           HISTORICO = HISTORICO||' Status Ativado.';
    END


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBGRADE', NEW.CODIGO, HISTORICO);
    END

END
^

SET TERM ; ^

/**********************************************************************/
/**********************************************************************/
/**********************************************************************/

UPDATE TBGRADE 
	SET ETQ01 = T01,
		ETQ02 = T02,
		ETQ03 = T03,
		ETQ04 = T04,
		ETQ05 = T05,
		ETQ06 = T06,
		ETQ07 = T07,
		ETQ08 = T08,
		ETQ09 = T09,
		ETQ10 = T10,
		ETQ11 = T11,
		ETQ12 = T12,
		ETQ13 = T13,
		ETQ14 = T14,
		ETQ15 = T15,
		ETQ16 = T16,
		ETQ17 = T17,
		ETQ18 = T18,
		ETQ19 = T19,
		ETQ20 = T20;