Reindex All Tables in a Database

I just wanted to share a quick script that I used to help re-index all the tables in my database. If you have any questions about it, please let me know! USE

Reindex All Tables in a Database

2008 August 22
by Shannon Lowder

I just wanted to share a quick script that I used to help re-index all the tables in my database. If you have any questions about it, please let me know!

USE 
GO
DECLARE @TableName varchar(255)

DECLARE TableCursor CURSOR FOR

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'base table'

OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName
	WHILE @@FETCH_STATUS = 0
	BEGIN
		PRINT 'Reindexing: ' + @TableName
		DBCC DBREINDEX(@TableName,' ',90)
	
		FETCH NEXT FROM TableCursor INTO @TableName
	END
	
CLOSE TableCursor
DEALLOCATE TableCursor 

Read more