
/*------ ATD 149995 ------*/
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'CONTROLESERIAL' AND COLUMN_NAME = 'NUMEROSERIALPAI')
ALTER TABLE CONTROLESERIAL ALTER COLUMN  NUMEROSERIALPAI VARCHAR(35)

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'CONTROLERMA' AND COLUMN_NAME = 'NUMSERIAL')
ALTER TABLE CONTROLERMA ALTER COLUMN NUMSERIAL VARCHAR(35)

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'CONTROLERMA' AND COLUMN_NAME = 'NUMSERIALRETORNO')
ALTER TABLE CONTROLERMA ALTER COLUMN NUMSERIALRETORNO VARCHAR(35)

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'CONTROLERMA' AND COLUMN_NAME = 'NUMSERIALSAIDA')
ALTER TABLE CONTROLERMA ALTER COLUMN NUMSERIALSAIDA VARCHAR(35)

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'SERIALTEXTOTECNICO' AND COLUMN_NAME = 'NUMEROSERIAL')
ALTER TABLE SERIALTEXTOTECNICO ALTER COLUMN NUMEROSERIAL VARCHAR(35)

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'CONTRATOSERVICO' AND COLUMN_NAME = 'NUMEROSERIAL')
ALTER TABLE CONTRATOSERVICO ALTER COLUMN NUMEROSERIAL VARCHAR(35)

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'COMPONENTE_TROCA' AND COLUMN_NAME = 'RSERIAL')
ALTER TABLE COMPONENTE_TROCA ALTER COLUMN RSERIAL VARCHAR(35)

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'COMPONENTE_TROCA' AND COLUMN_NAME = 'RSERIALTROCA')
ALTER TABLE COMPONENTE_TROCA ALTER COLUMN RSERIALTROCA VARCHAR(35)

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MANUTENCAO' AND COLUMN_NAME = 'TWNUMEROSERIAL')
ALTER TABLE MANUTENCAO ALTER COLUMN TWNUMEROSERIAL VARCHAR(35)

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'HISTORICORMA' AND COLUMN_NAME = 'NUMSERIAL')
ALTER TABLE HISTORICORMA ALTER COLUMN NUMSERIAL VARCHAR(35)

IF EXISTS (SELECT 1 FROM sysobjects WHERE NAME = 'pk_inventarioserial' )
alter table INVENTARIOCONTROLESERIAL drop constraint PK_INVENTARIOSERIAL
GO
IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE name='DF_INVENTARIOCONTROLESERIAL_NUMEROSERIAL')
ALTER TABLE INVENTARIOCONTROLESERIAL DROP CONSTRAINT DF_INVENTARIOCONTROLESERIAL_NUMEROSERIAL
GO
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'INVENTARIOCONTROLESERIAL' AND COLUMN_NAME = 'NUMEROSERIAL')
ALTER TABLE INVENTARIOCONTROLESERIAL ALTER COLUMN NUMEROSERIAL VARCHAR(35) NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM sysobjects WHERE NAME = 'pk_inventarioserial' )
ALTER TABLE INVENTARIOCONTROLESERIAL WITH NOCHECK ADD 
 CONSTRAINT PK_INVENTARIOSERIAL PRIMARY KEY  
 (FILIAL,CONTAGEM,CODPRO,NUMEROSERIAL) 
GO
IF EXISTS (SELECT 1 FROM sysindexes WHERE NAME = 'IDX_CONTROLESERIAL1' )
drop index CONTROLESERIAL.IDX_CONTROLESERIAL1
GO
IF EXISTS (SELECT 1 FROM sysindexes WHERE NAME = 'IDX_CONTROLESERIAL5' )
drop index CONTROLESERIAL.IDX_CONTROLESERIAL5
GO
IF EXISTS (SELECT 1 FROM sysindexes WHERE NAME = 'IDX_CONTROLESERIAL4' )
drop index CONTROLESERIAL.IDX_CONTROLESERIAL4
GO
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'CONTROLESERIAL' AND COLUMN_NAME = 'NUMEROSERIAL')
ALTER TABLE CONTROLESERIAL ALTER COLUMN NUMEROSERIAL VARCHAR(35) NOT NULL
go
IF NOT EXISTS (SELECT 1 FROM sysindexes WHERE NAME = 'IDX_CONTROLESERIAL1' )
CREATE NONCLUSTERED INDEX IDX_CONTROLESERIAL1 ON ControleSerial(CodPro, NumeroSerial,	Data)
GO
IF NOT EXISTS (SELECT 1 FROM sysindexes WHERE NAME = 'IDX_CONTROLESERIAL4' )
CREATE NONCLUSTERED INDEX IDX_CONTROLESERIAL4 ON ControleSerial(filial ) INCLUDE ( CodPro,NumeroSerial) 
GO
IF NOT EXISTS (SELECT 1 FROM sysindexes WHERE NAME = 'IDX_CONTROLESERIAL5' )
CREATE NONCLUSTERED INDEX IDX_CONTROLESERIAL5 ON ControleSerial(NumeroSerial )
GO