Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql server reindex script issue

Status
Not open for further replies.

edgare

IS-IT--Management
Jul 27, 2005
22
US
Hi guys
I found this script on the net to reindex and defrag my databases... doesn’t seem to work ... I am not a programmer so any help would be appreciated. Also is there a way to rebuild them or does this do that to?


/*
Defrag or re-index all indecies
*/

DECLARE @TableName sysname
DECLARE @IndexName VARCHAR(100)
DECLARE @indid int
DECLARE cur_tblfetch CURSOR FOR
SELECT name from sysobjects where xtype='U'

OPEN cur_tblfetch
FETCH NEXT FROM cur_tblfetch INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE cur_indfetch CURSOR FOR
SELECT indid,name FROM sysindexes WHERE id = OBJECT_ID (@TableName) and keycnt > 0
OPEN cur_indfetch
FETCH NEXT FROM cur_indfetch INTO @indid,@IndexName
WHILE @@FETCH_STATUS = 0
BEGIN
print 'Maintenancing ' + (@TableName) + '.' + (@IndexName)
--IF @indid <> 255 DBCC INDEXDEFRAG (0, @TableName, @indid)
IF @indid <> 255 DBCC DBREINDEX (@TableName, @IndexName)
FETCH NEXT FROM cur_indfetch INTO @indid,@IndexName
END
CLOSE cur_indfetch
DEALLOCATE cur_indfetch
FETCH NEXT FROM cur_tblfetch INTO @TableName
END
CLOSE cur_tblfetch
DEALLOCATE cur_tblfetch
 
What error does it give you?

Denny
MCSA (2003) / MCDBA (SQL 2000)

--Anything is possible. All it takes is a little research. (Me)

[noevil]
(Not quite so old any more.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top