IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[MailXchange]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
CREATE TABLE [dbo].[MailXchange](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[FromAddress] [varchar](max) NULL,
	[ToAddress] [varchar](max) NULL,
	[CCAddress] [varchar](max) NULL,
	[BCCAddress] [varchar](max) NULL,
	[Subject] [varchar](max) NULL,
	[MailType] [varchar](255) NULL,
	[LoginHotelCustomerNumber] [int] NULL,
	[ErrorMessage] [varchar](max) NULL,
	[ExeFilename] [varchar](max) NULL,
	[LoginUserIDNumber] [int] NULL,
	[BodyBinary] [varbinary](max) NULL,
	[FromName] [varchar](max) NULL,
	[HotelNumber] [int] NULL,
 CONSTRAINT [PK_MailXchange] PRIMARY KEY CLUSTERED ([ID] ASC)
) ON [PRIMARY]
GO

IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[MailXchangeAttachments]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
CREATE TABLE [dbo].[MailXchangeAttachments](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Filename] [varchar](max) NULL,
	[AttachmentBinary] [varbinary](max) NULL,
 CONSTRAINT [PK_MailXchangeAttachments] PRIMARY KEY CLUSTERED ([ID] ASC)
) ON [PRIMARY]
GO

IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[MailXchangeXtab]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
CREATE TABLE [dbo].[MailXchangeXtab](
	[MailXchangeID] [int] NULL,
	[MailXchangeAttachmentID] [int] NULL
) ON [PRIMARY]
GO

IF NOT EXISTS
(
  SELECT SysColumns.Name 
  FROM SysObjects WITH (NOLOCK) 
    INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID 
    AND SysColumns.Name = 'CreateTimestamp'
    WHERE SysObjects.Name = 'MailXchange'
)
  ALTER TABLE MailXchange ADD CreateTimestamp DATETIME NOT NULL DEFAULT GETDATE()
GO
