if exists (Select * from dbo.SysColumns Where id = object_id(N'Lote') and 
                                                 Name Like 'IdWebService' And
                                                 OBJECTPROPERTY(id, N'IsUserTable') = 1 )
begin

	DECLARE @Comando varchar(200)

	SELECT 
		@Comando = 'ALTER TABLE [dbo].['+OBJECT_NAME(fk.parent_object_id)+'] DROP CONSTRAINT ['+fk.name+'];'
	FROM 
		sys.foreign_keys AS fk
		INNER JOIN sys.foreign_key_columns AS fkc ON fk.OBJECT_ID = fkc.constraint_object_id
	  WHERE fk.parent_object_id = OBJECT_ID('Lote')
	  and COL_NAME(fkc.referenced_object_id, fkc.referenced_column_id) = 'IdWebService'
	  
	exec (@Comando)

	ALTER Table Lote
	DROP COLUMN IdWebService
end
GO

if not exists (Select * from dbo.SysColumns Where id = object_id(N'Lote') and 
                                                 Name Like 'TipoAmbiente' And
                                                 OBJECTPROPERTY(id, N'IsUserTable') = 1 )
begin
	alter table Lote
	add TipoAmbiente int
	
	alter table Lote
	add TipoServico int	
end
GO

Update Lote Set TipoAmbiente = 0 
where TipoAmbiente is null
GO
Update Lote Set TipoServico = 0 
where TipoServico is null
GO

Alter Table Lote Alter Column TipoAmbiente int Not Null
GO
Alter Table Lote Alter Column TipoServico int Not Null
GO


Create Table Versao
(Versao smallint not null, SubVersao smallint not null)
GO

alter Table Versao Add constraint PK_Versao primary key (Versao, SubVersao)
GO

Insert Into Versao Values (3, 10)
GO



