
/*------ ATD 445895 ------*/
IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'DIVISAODAVENDA' AND COLUMN_NAME = 'TWCODVEND')
ALTER TABLE DIVISAODAVENDA ADD TWCODVEND CHAR(5)
go

IF NOT EXISTS (SELECT 1 FROM ATRIBUTO WHERE CID = 154 AND NOME = 'TWCODVEND')
insert into ATRIBUTO (CID, NOME, NOMEEXTERNO, CIDREFERENCIADO, ATITUDE, TIPO, DIRECIONAVEL) values (154, 'TWCODVEND', 'TWCODVEND', 4, 0,25, 0)
go

IF EXISTS (SELECT name FROM sysobjects WHERE name = 'DIVISAODAVENDA_R' AND type = 'V')
   drop view DIVISAODAVENDA_R
go
create view dbo.DIVISAODAVENDA_R (
    PERCENTUAL,
    TIPODEVENDEDOR,
    TWCODVEND,
    RVENDEDOR,
    RDOCDEORIGEM,
    OID)
as select
    DIVISAODAVENDA.PERCENTUAL,
    DIVISAODAVENDA.TIPODEVENDEDOR,
    DIVISAODAVENDA.TWCODVEND,
    DIVISAODAVENDA.RVENDEDOR,
    DIVISAODAVENDA.RDOCDEORIGEM,
    DIVISAODAVENDA.OID
from
    DIVISAODAVENDA
go

IF EXISTS (SELECT name FROM sysobjects WHERE name = 'DIVISAODAVENDA_V' AND type = 'V')
   drop view DIVISAODAVENDA_V
go
create view dbo.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
go