
/*------ ATD 429977 ------*/
IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'SUBSTENTRADA' AND COLUMN_NAME = 'ID') 
BEGIN
	ALTER TABLE SUBSTENTRADA ADD ID INT NOT NULL IDENTITY(1,1)
	
	DECLARE @NOME_PK VARCHAR(50)
	
	SELECT @NOME_PK = ISNULL(MAX(constraint_name),'')
	  FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
     WHERE TABLE_NAME = 'SUBSTENTRADA'
       AND OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1
	   
	IF ISNULL(LEN(@NOME_PK),0) > 0
	BEGIN
		EXECUTE('ALTER TABLE SUBSTENTRADA DROP CONSTRAINT ' + @NOME_PK)
	END 
	EXECUTE('ALTER TABLE SUBSTENTRADA ADD CONSTRAINT PK_SUBSTENTRADA PRIMARY KEY (ID)')
	EXECUTE('CREATE INDEX IDX_SUBSTENTRADA1 ON SUBSTENTRADA(CODPRO, CF, ESTADOORIGEM, ESTADODESTINO, TPO)')
END
GO

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'ITNFENTCAD' AND COLUMN_NAME = 'IDSUBSTENTRADA') 
BEGIN
	ALTER TABLE ITNFENTCAD ADD IDSUBSTENTRADA INT NULL
END