I can run the below script within Query Analyzer but when I try to run it on a schedule it fails. Any help is appreciated.
-- Enter the name of the database you intend to update
-- USE DatabaseName
-- Rebuild indexes of all tables with a fill-factor of 90%
DECLARE
@TableName varchar(255)
DECLARE TableCursor CURSOR FOR
SELECT table_name
FROM information_schema.tables
WHERE table_type = 'base table'
PRINT 'Start reindexing ...'
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO
@TableName@FETCH_STATUS = 0
BEGIN
PRINT 'Reindexing ' +
@TableNameDBCC DBREINDEX(
@TableName, ' ', 90 )
FETCH NEXT FROM TableCursor INTO
@TableNameEND
CLOSE TableCursor
PRINT 'End reindexing'
DEALLOCATE TableCursor
-- Update statistics of all tables
EXEC sp_updatestats