• I've just used this to make it work with large table names and index names, as the max number of characters is 128 for an identifier.

    -- Build SQL to read each page in each index (including clustered index).

    SELECT @CheckIndexesSQL = @CheckIndexesSQL + CHAR(10) + 'SELECT COUNT_BIG(*) AS [' + LEFT(SchemaName + '.' + TableName + '.'

    + ISNULL(IndexName, 'HEAP') + '. IxId:' + CAST(indid AS VARCHAR(3)), 128) + '] FROM ' + QUOTENAME(SchemaName) + '.' + QUOTENAME(TableName)

    + ' WITH (INDEX(' + CAST(indid AS VARCHAR(3)) + '));'

    FROM #IndexDetails