
/*------ ATD 445895 ------*/
DECLARE v_count NUMBER:=0;

BEGIN

SELECT Count(1) INTO v_count FROM USER_TAB_COLUMNS WHERE UPPER(table_name) = 'DIVISAODAVENDA' and UPPER(column_name) = 'TWCODVEND';

IF v_count = 0 THEN

EXECUTE IMMEDIATE 'ALTER TABLE DIVISAODAVENDA ADD TWCODVEND CHAR(5)';

END IF;

END;
/

CREATE OR REPLACE VIEW DIVISAODAVENDA_R (
    PERCENTUAL,
    TIPODEVENDEDOR,
    TWCODVEND,
    RVENDEDOR,
    RDOCDEORIGEM,
    OID)
as select
    DIVISAODAVENDA.PERCENTUAL,
    DIVISAODAVENDA.TIPODEVENDEDOR,
    DIVISAODAVENDA.TWCODVEND,
    DIVISAODAVENDA.RVENDEDOR,
    DIVISAODAVENDA.RDOCDEORIGEM,
    DIVISAODAVENDA.OID
from
    DIVISAODAVENDA;
/

CREATE OR REPLACE VIEW DIVISAODAVENDA_V (
    RVENDEDOR,
    RDOCDEORIGEM,
    PERCENTUAL,
    TIPODEVENDEDOR,
    TWCODVEND,
    OID,
    NVENDEDOR,
    NDOCDEORIGEM
) as select
    DIVISAODAVENDA.RVENDEDOR,
    DIVISAODAVENDA.RDOCDEORIGEM,
    DIVISAODAVENDA.PERCENTUAL,
    DIVISAODAVENDA.TIPODEVENDEDOR,
    DIVISAODAVENDA.TWCODVEND,
    DIVISAODAVENDA.OID,
    T1.NOME,
    T2.NOME
from
    DIVISAODAVENDA,ITEM T1,ITEM T2
where
    DIVISAODAVENDA.RVENDEDOR = T1.OID AND
    DIVISAODAVENDA.RDOCDEORIGEM = T2.OID;
/