Created
September 9, 2015 18:28
-
-
Save christierney402/bc86a2d6824659e791d6 to your computer and use it in GitHub Desktop.
Rebuild all indexes for a database on MS SQL 2005-2008
This file contains hidden or 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
-- credit: http://blog.sqlauthority.com/2009/01/30/sql-server-2008-2005-rebuild-every-index-of-all-tables-of-database-rebuild-index-with-fillfactor/ | |
DECLARE @TableName VARCHAR(255) | |
DECLARE @sql NVARCHAR(500) | |
DECLARE @fillfactor INT | |
SET @fillfactor = 80 | |
DECLARE TableCursor CURSOR FOR | |
SELECT OBJECT_SCHEMA_NAME([object_id])+'.'+name AS TableName | |
FROM sys.tables | |
OPEN TableCursor | |
FETCH NEXT FROM TableCursor INTO @TableName | |
WHILE @@FETCH_STATUS = 0 | |
BEGIN | |
SET @sql = 'ALTER INDEX ALL ON ' + @TableName + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR(3),@fillfactor) + ')' | |
EXEC (@sql) | |
FETCH NEXT FROM TableCursor INTO @TableName | |
END | |
CLOSE TableCursor | |
DEALLOCATE TableCursor | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment