Rebuild Index using Query in MS-Sqlserver


In MS-Sqlserver by using below query to rebuild index of the tables.

DECLARE @TableName varchar(255)
DECLARE @TableSchema varchar(100)
DECLARE TableCursor CURSOR FOR

SELECT TABLE_SCHEMA,TABLE_NAME  FROM information_schema.tables
WHERE table_type = ‘base table
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableSchema,  @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
set @TableName=@TableSchema+’.’+@TableName
DBCC DBREINDEX(@TableName,’ ‘,100)
FETCH NEXT FROM TableCursor INTO @TableSchema,  @TableName
END
CLOSE TableCursor
DEALLOCATE TableCursor

Advertisements
This entry was posted in Articles and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s