Probably it's better not to use cursors. Try this one instead:
DECLARE @SQL NVarchar(4000)
SET @SQL = ''
SELECT @SQL = @SQL + 'EXEC ' + NAME + '..sp_MSforeachtable @command1=''DBCC DBREINDEX (''''*'''')'', @replacechar=''*''' + Char(13) FROM MASTER..Sysdatabases WHERE [name] = 'DatabaseName'
PRINT @SQL
EXEC (@SQL)
Main Topics
Browse All Topics





by: danrosenthalPosted on 2008-08-14 at 16:18:27ID: 22235068
Rebuilding Indexes with your script will lock tables one at a time (if they are clustered indexes). I would recommend finding a low usage time to rebuild the indexes on a regular basis.
Another option would be to use DBCC INDEXDEFRAG which is not as effective, but does not lock the tables and could be used in between full rebuilding.