USE [HCITools]
GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
IF EXISTS (SELECT 1 FROM master.cfg.InstanceContext where Business = 'TPCENT')
BEGIN

  IF NOT EXISTS (SELECT 1
                  FROM sys.objects o WITH (NOLOCK)
                  JOIN sys.schemas s WITH (NOLOCK) ON o.schema_id = s.schema_id
                 WHERE o.name = 'All_Stats_index'
                   AND o.type IN (N'U')
                   AND s.name = 'dbo')
  BEGIN

    CREATE TABLE [dbo].[All_Stats_index](
      [SI_ServerName] [sysname] NOT NULL,
      [Stats_index_ID] [int] NOT NULL,
      [SI_schemaname] [sysname] NOT NULL,
      [SI_databasename] [sysname] NOT NULL,
      [SI_tablename] [sysname] NOT NULL,
      [SI_indexname] [sysname] NULL,
      [SI_indextype] [nvarchar](60) NULL,
      [SI_user_seeks] [bigint] NULL,
      [SI_user_scans] [bigint] NULL,
      [SI_user_lookups] [bigint] NULL,
      [SI_user_updates] [bigint] NULL,
      [SI_last_user_seek] [datetime] NULL,
      [SI_last_user_scan] [datetime] NULL,
      [SI_last_user_lookup] [datetime] NULL,
      [SI_last_user_update] [datetime] NULL,
      [SI_updatedate] [datetime] NULL,
      [SI_restartdate] [datetime] NULL,
      [SI_deletedate] [datetime] NULL,
    CONSTRAINT [PK_All_Stats_index_1] PRIMARY KEY CLUSTERED 
    (
	  [SI_ServerName] ASC,
	  [Stats_index_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]
  END

  IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[All_Stats_index]') AND name = N'IDX_SI')
  BEGIN
    CREATE NONCLUSTERED INDEX [IDX_SI] ON [dbo].[All_Stats_index] 
    (
	  [SI_databasename] ASC,
	  [SI_tablename] ASC
    )
    INCLUDE ( [SI_ServerName],
    [Stats_index_ID]) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  END
END
GO


