

IF   EXISTS (  SELECT * 
				 FROM INFORMATION_SCHEMA.COLUMNS  
				WHERE TABLE_NAME  = 'BC_CONCILICAO_EXTRATO_DDA' 
				  AND COLUMN_NAME  = 'CD_BC_LANC' 
				  AND IS_NULLABLE  = 'YES'
				 ) 
BEGIN 
-- evitar de dar erro ao executar o script pela primeira vez. 
	UPDATE BC_CONCILICAO_EXTRATO_DDA SET CD_BC_LANC = 0 WHERE CD_BC_LANC IS NULL;
	
	ALTER TABLE BC_CONCILICAO_EXTRATO_DDA ALTER COLUMN CD_BC_LANC INT NOT NULL 
END 
GO

IF NOT EXISTS (SELECT   * 
				 FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
				WHERE CONSTRAINT_NAME  = 'PK_BC_CONCILICAO_EXTRATO_DDA' 
				  AND COLUMN_NAME  = 'CD_BC_LANC' 
				 ) 
BEGIN 
	
	ALTER TABLE BC_CONCILICAO_EXTRATO_DDA DROP CONSTRAINT PK_BC_CONCILICAO_EXTRATO_DDA
	ALTER TABLE BC_CONCILICAO_EXTRATO_DDA ADD CONSTRAINT PK_BC_CONCILICAO_EXTRATO_DDA PRIMARY KEY (NR_LOTE, CD_BC, DT_MOV, ORDEM,    CD_BC_LANC)

END 