
IF EXISTS (SELECT 1
               FROM   INFORMATION_SCHEMA.COLUMNS
               WHERE  TABLE_NAME = 'T_EMAIL_MESSAGE'
                      AND COLUMN_NAME = 'IsPack'
                      AND TABLE_SCHEMA='vclib')
BEGIN
	
       exec('insert into vclib.T_SCHEDULED_REPORT(OperationID, IsPack, [Password])
              select t1.OperationID, t1.IsPack, t1.[Password]
              from vclib.T_EMAIL_MESSAGE t1')
END
go
IF EXISTS (SELECT 1
               FROM   INFORMATION_SCHEMA.COLUMNS
               WHERE  TABLE_NAME = 'T_EMAIL_MESSAGE'
                      AND COLUMN_NAME = 'IsPack'
                      AND TABLE_SCHEMA='vclib')
alter table vclib.T_EMAIL_MESSAGE
drop column IsPack
go
IF EXISTS (SELECT 1
               FROM   INFORMATION_SCHEMA.COLUMNS
               WHERE  TABLE_NAME = 'T_EMAIL_MESSAGE'
                      AND COLUMN_NAME = 'Password'
                      AND TABLE_SCHEMA='vclib')
alter table vclib.T_EMAIL_MESSAGE
drop column [Password]
GO
if exists(select * from sys.foreign_keys fk where fk.name = 'FK_T_EMAIL_MESSAGE_T_EMAIL_ATTACHMENT_EMAILMESSAGE')
ALTER TABLE [VCLib].[T_EMAIL_ATTACHMENT] DROP CONSTRAINT [FK_T_EMAIL_MESSAGE_T_EMAIL_ATTACHMENT_EMAILMESSAGE]
GO
if exists(select * from sys.foreign_keys fk where fk.name = 'FK_T_SCHEDULED_REPORT_T_EMAIL_ATTACHMENT_EMAILMESSAGE')
ALTER TABLE [VCLib].[T_EMAIL_ATTACHMENT] DROP CONSTRAINT [FK_T_SCHEDULED_REPORT_T_EMAIL_ATTACHMENT_EMAILMESSAGE]
GO
ALTER TABLE [VCLib].[T_EMAIL_ATTACHMENT]  WITH NOCHECK ADD  CONSTRAINT [FK_T_SCHEDULED_REPORT_T_EMAIL_ATTACHMENT_EMAILMESSAGE] FOREIGN KEY([EmailMessage])
REFERENCES [VCLib].[T_SCHEDULED_REPORT] ([OperationID])
GO
ALTER TABLE [VCLib].[T_EMAIL_ATTACHMENT] CHECK CONSTRAINT [FK_T_SCHEDULED_REPORT_T_EMAIL_ATTACHMENT_EMAILMESSAGE]
GO