Created
August 24, 2014 10:56
-
-
Save FreekPaans/6bca7c97af87c56aab59 to your computer and use it in GitHub Desktop.
rebuild all indices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @DatabaseName SYSNAME = DB_NAME(), @TableName VARCHAR(256) | |
DECLARE @DynamicSQL NVARCHAR(max) = | |
'DECLARE curAllTablesInDB CURSOR FOR SELECT TABLE_SCHEMA + | |
''.'' + TABLE_NAME AS TABLENAME | |
FROM ' + @DatabaseName + '.INFORMATION_SCHEMA.TABLES WHERE | |
TABLE_TYPE = ''BASE TABLE''' | |
BEGIN | |
EXEC sp_executeSQL @DynamicSQL -- create tables cursor | |
OPEN curAllTablesInDB | |
FETCH NEXT FROM curAllTablesInDB INTO @TableName | |
WHILE (@@FETCH_STATUS = 0) | |
BEGIN | |
SET @DynamicSQL = 'ALTER INDEX ALL ON ' + @TableName + ' REBUILD' | |
PRINT @DynamicSQL | |
EXEC sp_executeSQL @DynamicSQL | |
FETCH NEXT FROM curAllTablesInDB INTO @TableName | |
END -- cursor WHILE | |
CLOSE curAllTablesInDB | |
DEALLOCATE curAllTablesInDB | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment