
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'EST_PBM_PROJETO_LOG') AND type in (N'U'))
AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'EST_PBM_PROJETO_LOG' AND COLUMN_NAME = 'CD_USU')
BEGIN

   ALTER TABLE EST_PBM_PROJETO_LOG 
      ADD CD_USU INT NULL

END
GO

UPDATE EST_PBM_PROJETO_LOG
   SET CD_USU = 1
   WHERE CD_USU IS NULL
GO

ALTER TABLE EST_PBM_PROJETO_LOG 
   ALTER COLUMN CD_USU INT NOT NULL 
GO

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_EST_PBM_PROJETO_LOG_GLB_USU]') AND parent_object_id = OBJECT_ID(N'[dbo].[EST_PBM_PROJETO_LOG]'))
BEGIN

   ALTER TABLE [dbo].[EST_PBM_PROJETO_LOG]  WITH CHECK ADD  CONSTRAINT [FK_EST_PBM_PROJETO_LOG_GLB_USU] FOREIGN KEY([CD_USU])
   REFERENCES [dbo].[GLB_USU] ([CD_USU])

END
GO