Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tsql to know when dbcc checkdb was run on a database last time

Using Tsql, how can i know when DBCC checkdb was last run on SQL server (2000, 2005 or 2008)?

Regards

like image 451
Manjot Avatar asked May 13 '10 23:05

Manjot


People also ask

When was Checkdb last run?

Approximately half-way down, at line 51, you'll see a line where the third column (called Field in the output) shows dbi_dbccLastKnownGood. That's what you're looking for. The forth column for that row, called Value, shows the date that DBCC CHECKDB last completed successfully.

How can I check my DBCC Checkdb status?

If you see DBCC ALLOC CHECK, wait a bit as the DBCC TABLE CHECK occurs after the ALLOC CHECK. If you ran CheckDB in SSMS you don't need to run the first query, you can just look at the name of the tab in SSMS to find the sessionID.

How long will Checkdb take to run?

FULL CHECKDB with 64 cores: takes 7 minutes, checks everything.

How often should I run DBCC Checkdb?

The shorter the period of time you keep backups, the more often you need to run DBCC CHECKDB. If you keep data for two weeks, weekly is a good starting point. If you take weekly fulls, you should consider running your DBCC checks before those happen. A corrupt backup doesn't help you worth a lick.


2 Answers

on 2005 and up you can run

DBCC DBINFO ('YourDatabaseName') WITH TABLERESULTS

look for dbi_dbccLastKnownGood

like image 71
SQLMenace Avatar answered Sep 28 '22 15:09

SQLMenace


Try this to check all DB's on SQL 2005 onwards

SET NOCOUNT ON;
DBCC TRACEON (3604);

CREATE TABLE #temp (
        Id INT IDENTITY(1,1), 
        ParentObject VARCHAR(255),
        [Object] VARCHAR(255),
        Field VARCHAR(255),
        [Value] VARCHAR(255)
)

CREATE TABLE #Results (
        DBName VARCHAR(255),
        LastGoodDBCC VARCHAR(255)
)

DECLARE @Name VARCHAR(255);

DECLARE looping_cursor CURSOR
FOR

SELECT name
FROM master.dbo.sysdatabases
WHERE CONVERT(varchar(500),databasepropertyex(name, 'Status'),0) = 'ONLINE'

OPEN looping_cursor
FETCH NEXT FROM looping_cursor INTO @Name
WHILE @@FETCH_STATUS = 0
    BEGIN

        INSERT INTO #temp
        EXECUTE('DBCC PAGE (['+@Name+'], 1, 9, 3)WITH TABLERESULTS');

        INSERT INTO #Results
        SELECT @Name,MAX(VALUE) FROM #temp
        WHERE Field = 'dbi_dbccLastKnownGood';

    FETCH NEXT FROM looping_cursor INTO @Name
    END
CLOSE looping_cursor;
DEALLOCATE looping_cursor;

SELECT DBName
    ,ISNULL(LastGoodDBCC,'1900-01-01 00:00:00.000') AS 'LastGoodDBCC'
FROM #Results

DROP TABLE #temp
DROP TABLE #Results
like image 36
JP Schonfeldt Avatar answered Sep 28 '22 16:09

JP Schonfeldt