IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[ReservationSendLetter]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN

	CREATE TABLE [dbo].[ReservationSendLetter](
		[ID] [int] IDENTITY(1,1) NOT NULL,
		[FK_ReferenceNumber] [int] NOT NULL,
		[FK_GuestNumber] [smallint] NOT NULL,
		[DoSend] [INT] NOT NULL,
		[TemplateMode] [int] NOT NULL,
		[Status] [int] NOT NULL,
		CreateDatestamp Datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
		LastUpdDatestamp Datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
	CONSTRAINT [PK_ReservationSendLetter] PRIMARY KEY CLUSTERED 
	(
	[ID] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE [dbo].[ReservationSendLetter] ADD  CONSTRAINT [DF_ReservationSendLetter_DoSend]  DEFAULT ((0)) FOR [DoSend]

	ALTER TABLE [dbo].[ReservationSendLetter] ADD  CONSTRAINT [DF_ReservationSendLetter_TemplateMode]  DEFAULT ((0)) FOR [TemplateMode]

	ALTER TABLE [dbo].[ReservationSendLetter] ADD  CONSTRAINT [DF_ReservationSendLetter_Status]  DEFAULT ((0)) FOR [Status]


	ALTER TABLE [dbo].[ReservationSendLetter]  WITH CHECK ADD  CONSTRAINT [FK_ReservationSendLetter_Reservations] FOREIGN KEY([FK_ReferenceNumber], [FK_GuestNumber])
	REFERENCES [dbo].[Reservations] ([ReferenceNumber], [GuestNumber])

	ALTER TABLE [dbo].[ReservationSendLetter] CHECK CONSTRAINT [FK_ReservationSendLetter_Reservations]
	
	IF EXISTS
	(
	  SELECT SysColumns.Name 
	  FROM SysObjects WITH (NOLOCK) 
		INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID 
		AND SysColumns.Name = 'DoSendMail'
		WHERE SysObjects.Name = 'Reservations'
	)
	BEGIN
		EXEC('INSERT INTO ReservationSendLetter (FK_ReferenceNumber, FK_GuestNumber, DoSend, TemplateMode)
		SELECT ReferenceNumber, GuestNumber, DoSendMail, SendMailTemplateDefaultType FROM Reservations (NOLOCK)
		WHERE DoSendMail = 1')

		DECLARE @ConstraintName VARCHAR(100)

		select @ConstraintName = d.name from sys.tables t (nolock)
		inner join sys.columns c (nolock) ON c.object_id = t.object_id AND t.Name = 'Reservations' AND c.Name = 'DoSendMail'
		inner join Sys.default_constraints d (nolock) ON c.column_id = d.parent_column_id AND c.object_id = d.parent_object_id

		EXEC('ALTER TABLE Reservations DROP CONSTRAINT ' + @ConstraintName + ' , COLUMN DoSendMail')
		ALTER TABLE Reservations DROP COLUMN DoSendMail

	END
END
ELSE
BEGIN
	IF NOT EXISTS
	(
	  SELECT SysColumns.Name 
	  FROM SysObjects WITH (NOLOCK) 
		INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID 
		AND SysColumns.Name = 'CreateDatestamp'
		WHERE SysObjects.Name = 'ReservationSendLetter'
	)
	BEGIN
	  ALTER TABLE ReservationSendLetter ADD CreateDatestamp Datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
	END

	IF NOT EXISTS
	(
	  SELECT SysColumns.Name 
	  FROM SysObjects WITH (NOLOCK) 
		INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID 
		AND SysColumns.Name = 'LastUpdDatestamp'
		WHERE SysObjects.Name = 'ReservationSendLetter'
	)
	BEGIN
	  ALTER TABLE ReservationSendLetter ADD LastUpdDatestamp Datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
	END

END