Rebuilding database indexes

Use the following SQL script to rebuild all indexes in all License Metric Tool database tables.

USE TEMADB
SELECT
  'ALTER INDEX ALL ON ' + t.[TABLE_SCHEMA]
  + '.' + t.[TABLE_NAME] + ' REBUILD; '
FROM INFORMATION_SCHEMA.TABLES t
USE TEMADB
GO
IF EXISTS (SELECT
    *
  FROM dbo.imports
  WHERE success IS NULL)
BEGIN
  PRINT N'CANNOT RUN index rebuild. LMT import is running!'
  PRINT N'Wait until LMT import finishes'
END
ELSE
BEGIN
  DECLARE table_cursor CURSOR FOR
  SELECT
    table_schema,
    table_name
  FROM INFORMATION_SCHEMA.TABLES
  WHERE table_type = 'BASE TABLE'
  OPEN table_cursor
  DECLARE @tableName sysname
  DECLARE @tableSchema sysname
  FETCH NEXT FROM table_cursor INTO @tableSchema, @tableName
  WHILE @@fetch_status != -1
  BEGIN
    PRINT N'START alter index all on ' + @tableSchema
    + N'.' + @tableName + N' rebuild';
    EXECUTE (N'alter index all on ' + @tableSchema + N'.'
    + @tableName
    +
    N' rebuild')
    PRINT N'END alter index all on ' + @tableSchema
    + N'.' + @tableName + N' rebuild';
    FETCH NEXT FROM table_cursor INTO @tableSchema,
    @tableName
  END
  CLOSE table_cursor
  DEALLOCATE table_cursor
  PRINT N'START sp_updatestats';
  EXECUTE sp_updatestats
  PRINT N'END sp_updatestats';
END
GO