I am currently trying to script a job on SQL Server 2005 that will automate the DBCC CHECKDB process. Basically, I am using a cursor to run through and run DBCC CHECKDB on every database on an instance. Sometimes it works, running through every database and logging the errors in a table I have designed for that purpose and sometimes it only runs through a few of the databases and stop. Does anyone have any idea what is going on? I have included the code that I use for the cursor.
DECLARE @DbName varchar(100)
DECLARE GetDbName CURSOR LOCAL FORWARD_ONLY OPTIMISTIC FOR SELECT name FROM sys.databases ORDER BY name
OPEN GetDbName FETCH NEXT FROM GetDbName INTO @DbName WHILE (@@fetch_status = 0) BEGIN
print @DbName
INSERT INTO
TempLog
EXEC('DBCC CHECKDB ('+ @DbName +') WITH NO_INFOMSGS, TABLERESULTS')
FETCH NEXT FROM GetDbName
INTO @DbName
END
CLOSE GetDbName DEALLOCATE GetDbName