Search

Thursday, March 15, 2012

Run CheckDB on all Database

Use below script to run CheckDB on all the Database on Server.


Set Nocount on
Declare @Server_Name varchar(65) 
Declare @Database_Name varchar(65)


Declare Cur_DB Cursor For
        Select name from master.dbo.sysdatabases


Declare @osql varchar(2000)


select @Server_Name = @@Server_Name


Open Cur_DB
Fetch Next from Cur_DB into @Database_Name
While @@Fetch_status=0
    Begin
        Set @osql='DBCC Checkdb (['+@Database_Name+']) WITH PHYSICAL_ONLY'
        EXEC (@osql) --Execute the osql statement
        Fetch Next from Cur_DB into @Database_Name
    End
Close Cur_DB


Deallocate Cur_DB


GO

No comments:

Post a Comment