Rebuilds indexes for entire database

SQL Add comments

In some cases we may need to rebuild indexes for wholeover the database. There are many ways to rebuild indexes on table but one I like is using DBCC DBREINDEX statement. This one statement we reindex single or all indexes on table. But again this will be very time consuming when you want to rendex for all tables in database.

Hmm... Not to worry we can appy reindexing to all tables by below SQL code.

DECLARE @tablename varchar(200)
DECLARE table_cursor CURSOR FOR 
select [name] from sysobjects where type='U'

OPEN table_cursor

FETCH NEXT FROM table_cursor INTO @tablename

WHILE @@FETCH_STATUS = 0
BEGIN
    DBCC DBREINDEX (@tablename, '', 80);
    FETCH NEXT FROM table_cursor INTO @tablename
END 
CLOSE table_cursor
DEALLOCATE table_cursor

SQL statement 

select [name] from sysobjects where type='U' 
will get all tables name in database and just looping thourgh cursor will reindex in whole database.

Keep passion while running this query as it will take time to reindix all tabes.

0 responses to “Rebuilds indexes for entire database”

  1. Ads Says:

Leave a Reply




Powered by Mango Blog. Design and Icons by N.Design Studio | Menu Apycom
RSS Feeds