Last Time CheckDB was Run

Corrupt PagesBack in November 2012, I shared a story about checking the last known good checkdb in the boot page of a database.  You can read that story here.

This is an important enough topic that it is worth repeating frequently if I wanted to do that.  If  for no other reason than to continue to hammer at how important it is to both run checkdb and know the last time that checkdb was run successfully.

Alas, I am writing to fix a few things with the script that I shared in that last past.

I run this script on every server I touch to get a report for the last known good checkdb for every single database.  I had been running the script flawlessly across many servers without error.  Then it happened.  The script failed with a nasty error.

After a bit of looking, it became apparent my flaw in the script.  I had not written the script with CS (case sensitivity) in mind.  I touch so few CS servers, that I sometimes forget to check for that.  Slap my hands and I will work on that going forward.

So here is the update to the script.

[codesyntax lang=”tsql”]


9 thoughts on “Last Time CheckDB was Run”

  1. Great script. I’ve been meaning to rewrite mine as it uses sp_msforeachdb which has problems.

    Just curious, why do you filter out tempdb? Tempdb can get corrupted too.

    One suggestion, it fails if there is a database offline (mirror, log shipping secondary, etc). Recommend adding “state = 0” to the query of sys.databases.

    1. I waffled on filtering out offline databases or not. I’ll add it in. I was excluding tempdb only because each time the instance is restarted, boot page is restored and the dbi_dbccLastKnownGood is restored back to 1/1/1900.
      But I agree that tempdb can be corrupt and if the server hasn’t been restarted for a while then we need to know the last time checkdb was run against tempdb. I’ll change that filter.

  2. I actually wrote similar code and it got added to Brent Ozar’s sp_blitz procedure. Someone found a bug – not in my code, but in the output of DBCC DBInfo, which returns a row for dbi_dbccLastKnownGood twice in SQL 2008+. Looks like your version using DBCC Page doesn’t suffer from this problem. Nice.

  3. Correction..Looks like the bug still happens with DBCC Page (I tested on a 2005 server by mistake), but I see that’s why you select WHERE RowNum = 1…

    1. Yeah. I was seeing that bug. I had to back all the way out and examine the DBCC Page results to confirm on a single database to make sure it wasn’t something I had done. But the rownum was an easy enough fix.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.