IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[MISHistory]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
  CREATE TABLE [dbo].[MISHistory]
  (
    [SystemDate] [date] NOT NULL,
    [DateStamp] [date] NOT NULL,
    [FK_HotelCustomerNumber] [int] NOT NULL,
    [FK_DepartmentNumber] [smallint] NOT NULL,
    [FK_AccountNumber] [int] NOT NULL,
    [FK_SegmentNumber] [smallint] NOT NULL,
    [FK_PointOfSaleNumber] [smallint] NOT NULL,
    [FK_PurposeCodeNumber] [int] NOT NULL,
    [FK_GuestTypeNumber] [smallint] NOT NULL,
    [FK_ProdutNumber] [int] NOT NULL,
    [Room_HallType] [tinyint] NOT NULL,
    [RoomTypeNumber] [int] NOT NULL,
    [ValueType] [tinyint] NOT NULL,
    [Adult] [int] NULL,
    [Children1] [int] NULL,
    [Children2] [int] NULL,
    [NumberOfRooms] [int] NULL,
    [Price] [money] NULL,
    [VATOfPrice] [money] NULL,
    [Package] [money] NULL,
    [VATOfPackage] [money] NULL,
    [FK_WareNumber] [int] NULL,
    CONSTRAINT [PK_MISHistory] PRIMARY KEY CLUSTERED 
    (
      [SystemDate] ASC,
      [DateStamp] ASC,
      [FK_HotelCustomerNumber] ASC,
      [FK_DepartmentNumber] ASC,
      [FK_AccountNumber] ASC,
      [FK_SegmentNumber] ASC,
      [FK_PointOfSaleNumber] ASC,
      [FK_PurposeCodeNumber] ASC,
      [FK_GuestTypeNumber] ASC,
      [FK_ProdutNumber] ASC,
      [Room_HallType] ASC,
      [RoomTypeNumber] ASC,
      [ValueType] ASC
    )
  ) ON [PRIMARY]

  ALTER TABLE [dbo].[MISHistory]  WITH NOCHECK ADD  CONSTRAINT [FK_MISHistory_Accounts] FOREIGN KEY([FK_AccountNumber])
  REFERENCES [dbo].[Accounts] ([AccountNumber])

  ALTER TABLE [dbo].[MISHistory] NOCHECK CONSTRAINT [FK_MISHistory_Accounts]

  ALTER TABLE [dbo].[MISHistory]  WITH NOCHECK ADD  CONSTRAINT [FK_MISHistory_Customer] FOREIGN KEY([FK_HotelCustomerNumber])
  REFERENCES [dbo].[Customer] ([CustomerNumber])

  ALTER TABLE [dbo].[MISHistory] NOCHECK CONSTRAINT [FK_MISHistory_Customer]

  ALTER TABLE [dbo].[MISHistory]  WITH NOCHECK ADD  CONSTRAINT [FK_MISHistory_Departments] FOREIGN KEY([FK_DepartmentNumber])
  REFERENCES [dbo].[Departments] ([PK_Department])

  ALTER TABLE [dbo].[MISHistory] NOCHECK CONSTRAINT [FK_MISHistory_Departments]

  ALTER TABLE [dbo].[MISHistory]  WITH NOCHECK ADD  CONSTRAINT [FK_MISHistory_GuestTypes] FOREIGN KEY([FK_GuestTypeNumber])
  REFERENCES [dbo].[GuestTypes] ([TypeNumber])

  ALTER TABLE [dbo].[MISHistory] NOCHECK CONSTRAINT [FK_MISHistory_GuestTypes]

  ALTER TABLE [dbo].[MISHistory]  WITH NOCHECK ADD  CONSTRAINT [FK_MISHistory_PointOfSales] FOREIGN KEY([FK_PointOfSaleNumber])
  REFERENCES [dbo].[PointOfSales] ([Number])

  ALTER TABLE [dbo].[MISHistory] NOCHECK CONSTRAINT [FK_MISHistory_PointOfSales]

  ALTER TABLE [dbo].[MISHistory]  WITH NOCHECK ADD  CONSTRAINT [FK_MISHistory_Products] FOREIGN KEY([FK_ProdutNumber])
  REFERENCES [dbo].[Products] ([ProductNumber])

  ALTER TABLE [dbo].[MISHistory] NOCHECK CONSTRAINT [FK_MISHistory_Products]

  ALTER TABLE [dbo].[MISHistory]  WITH NOCHECK ADD  CONSTRAINT [FK_MISHistory_PurposeCodes] FOREIGN KEY([FK_PurposeCodeNumber])
  REFERENCES [dbo].[PurposeCodes] ([PurposeCodeNumber])

  ALTER TABLE [dbo].[MISHistory] NOCHECK CONSTRAINT [FK_MISHistory_PurposeCodes]

  ALTER TABLE [dbo].[MISHistory]  WITH NOCHECK ADD  CONSTRAINT [FK_MISHistory_Segments] FOREIGN KEY([FK_SegmentNumber])
  REFERENCES [dbo].[Segments] ([Number])

  ALTER TABLE [dbo].[MISHistory] NOCHECK CONSTRAINT [FK_MISHistory_Segments]
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 = 'FK_WareNumber'
      WHERE SysObjects.Name = 'MISHistory'
  )
  BEGIN
    ALTER TABLE MISHistory ADD FK_WareNumber INT NULL
  END
END