IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[TransactionPeriod]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
  CREATE TABLE [dbo].[TransactionPeriod]
  (
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [FK_TransactionNumber] [int] NULL,
    [FromDateTime] [datetime] NOT NULL,
    [ToDateTime] [datetime] NOT NULL,
    [DateStampSold] [datetime] NULL,
    [FK_WareNumber] [int] NULL,
    [Pieces] [int] NULL,
    [SessionId] [varchar](900) NULL,
    [SubscriptionArrangementLineNumber] int NULL,
    [TreaterConferenceRoomNumber] int NULL,
    [HallConferenceRoomNumber] int NULL
    CONSTRAINT [PK_TransactionPeriod] PRIMARY KEY CLUSTERED 
    (
      [Id] ASC
    )
  ) ON [PRIMARY]

  ALTER TABLE [dbo].[TransactionPeriod]  WITH NOCHECK ADD  CONSTRAINT [FK_TransactionPeriod_Transactions] FOREIGN KEY([FK_TransactionNumber])
  REFERENCES [dbo].[Transactions] ([TransactionNumber])
  NOT FOR REPLICATION 

  ALTER TABLE [dbo].[TransactionPeriod] NOCHECK CONSTRAINT [FK_TransactionPeriod_Transactions]

  ALTER TABLE [dbo].[TransactionPeriod]  WITH NOCHECK ADD  CONSTRAINT [FK_TransactionPeriod_Ware] FOREIGN KEY([FK_WareNumber])
  REFERENCES [dbo].[Ware] ([WareNumber])
  NOT FOR REPLICATION 

  ALTER TABLE [dbo].[TransactionPeriod] NOCHECK CONSTRAINT [FK_TransactionPeriod_Ware]
END
GO

IF NOT EXISTS
(
  SELECT *
  FROM SysObjects WITH (NOLOCK) 
  INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID AND SysColumns.Name = 'SubscriptionArrangementLineNumber'
  WHERE SysObjects.Name = 'TransactionPeriod'
)
BEGIN
  ALTER TABLE dbo.TransactionPeriod ADD SubscriptionArrangementLineNumber int NULL
END
GO
IF NOT EXISTS
(
  SELECT *
  FROM SysObjects WITH (NOLOCK) 
  INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID AND SysColumns.Name = 'TreaterConferenceRoomNumber'
  WHERE SysObjects.Name = 'TransactionPeriod'
)
BEGIN
  ALTER TABLE dbo.TransactionPeriod ADD TreaterConferenceRoomNumber int NULL
END
GO
IF NOT EXISTS
(
  SELECT *
  FROM SysObjects WITH (NOLOCK) 
  INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID AND SysColumns.Name = 'HallConferenceRoomNumber'
  WHERE SysObjects.Name = 'TransactionPeriod'
)
BEGIN
  ALTER TABLE dbo.TransactionPeriod ADD HallConferenceRoomNumber int NULL
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[TransactionPeriod]') AND name = N'IX_TransactionPeriod_TreaterConferenceRoomNumber')
CREATE NONCLUSTERED INDEX [IX_TransactionPeriod_TreaterConferenceRoomNumber] ON [dbo].[TransactionPeriod]
(
	[TreaterConferenceRoomNumber] ASC,
  [FromDateTime] ASC,
  [ToDateTime] ASC
)
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[TransactionPeriod]') AND name = N'IX_TransactionPeriod_HallConferenceRoomNumber')
CREATE NONCLUSTERED INDEX [IX_TransactionPeriod_HallConferenceRoomNumber] ON [dbo].[TransactionPeriod]
(
	[HallConferenceRoomNumber] ASC,
  [FromDateTime] ASC,
  [ToDateTime] ASC
)
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[TransactionPeriod]') AND name = N'IX_TransactionPeriod_SubscriptionArrangementLineNumber')
CREATE NONCLUSTERED INDEX [IX_TransactionPeriod_SubscriptionArrangementLineNumber] ON [dbo].[TransactionPeriod]
(
	[SubscriptionArrangementLineNumber] ASC
)
GO