
IF NOT EXISTS(SELECT 1 FROM SYSCOLUMNS WHERE ID = OBJECT_ID('EST_PROD_TABLOIDE_FILIAL') AND NAME = 'DT_CAD')
BEGIN 
	ALTER TABLE DBO.EST_PROD_TABLOIDE_FILIAL ADD DT_CAD DATETIME NULL
END
GO

UPDATE A
SET A.DT_CAD = B.DT_CAD
FROM EST_PROD_TABLOIDE_FILIAL A
INNER JOIN EST_PROD_TABLOIDE B ON B.CD_EMP = A.CD_EMP AND B.CD_TABLOIDE = A.CD_TABLOIDE
WHERE A.DT_CAD IS NULL

IF NOT EXISTS (SELECT * FROM sys.default_constraints WHERE name = N'DF_EST_PROD_TABLOIDE_FILIAL_DT_CAD')
begin
	ALTER TABLE [DBO].[EST_PROD_TABLOIDE_FILIAL] 
		ADD CONSTRAINT [DF_EST_PROD_TABLOIDE_FILIAL_DT_CAD] DEFAULT (GETDATE()) FOR [DT_CAD]
END

ALTER TABLE [DBO].[EST_PROD_TABLOIDE_FILIAL] 
	ALTER COLUMN [DT_CAD] DATETIME NOT NULL
