Anatoly Lubarsky Logo
MSSQL, .NET, Design. Life and Music

MSSQL Tip: Rebuild Clustered Indexes

Rebuild clustered index in the database tables. This code uses cursor to run through user tables. Also it uses dbcc dbreindex command to rebuild index. Use this with care since each rebuild hangs table up.


declare tabcurs cursor
for
   select name
   from sysobjects
   where xtype = 'u'
open tabcurs
declare @tname varchar(30)
fetch next from tabcurs into @tname
while @@fetch_status = 0
begin
   dbcc dbreindex(@tname)
   fetch next from tabcurs into @tname
end
close tabcurs
deallocate tabcurs

Related Posts:

Sunday, November 30, 2003 1:52 PM

If your feedback doesn't appear right away, please be patient as it may take a few minutes to publish.

Post a Comment

Protected by CAPTCHAEnter the code you see
Name (*)  
E-mail (*)  
Url
Remember

Comment (*)