Monday, July 13, 2009

DBCC for All Databases

Another bit of code I've come up with to make maintenance life easier at my work: running DBCC CHECKDB and DBCC UPDATEUSAGE for all Great Plains companies. I know the Maintenance Plan can do this, but sometimes you just need to run it manually. If you have a lot of companies, this can be a chore.
declare @DatabaseId varchar(255)

declare DatabaseLoop cursor for 
    select name from master..sysdatabases 
--        where name in (select INTERID from DYNAMICS..SY01500 union select 'DYNAMICS')
--        Uncomment previous line for only GP databases

open DatabaseLoop
fetch next from DatabaseLoop into @DatabaseId
while (@@fetch_status <> -1)
    begin

    /*Inner Loop for each Database*/

        RAISERROR (@DatabaseId, 0, 1) WITH NOWAIT;
        WAITFOR DELAY '000:00:05'
        DBCC UPDATEUSAGE(@DatabaseId)
        DBCC CHECKDB (@DatabaseId) WITH NO_INFOMSGS
        print CHAR(13)+CHAR(10)+'----------------'+CHAR(13)+CHAR(10)

    fetch next from DatabaseLoop into @DatabaseId 
    end

close DatabaseLoop
deallocate DatabaseLoop

1 comment:

  1. Syntax was helpful, there i found an amazing article discussed briefly about DBCC UPDATEUSAGE. You may go through following link for getting brief description about DBCC UPDATEUSAGE:
    http://www.sqlmvp.org/running-dbcc-updateusage-in-sql-server/

    ReplyDelete