Last Time CheckDB was Run

Categories: Corner, News, Professional, SSC
Comments: 9 Comments
Published on: January 17, 2013

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 Comments - Leave a comment
  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.

    • Jason Brimhall says:

      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. Shaun says:

    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. Shaun says:

    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…

    • Jason Brimhall says:

      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.

  4. Rob Burton says:

    Thanks for the script, it’s pretty useful!

  5. […] runs regularly but maybe it didn’t run last night. That is easy enough to check with a little query from here. Since a consistency check does not seem to have run (as confirmed by the script) and is the second […]

  6. […] script off of their site, I’ll point you straight toward Jason Brimhall’s blog Last Time CheckDB Was Run. His script uses the undocumented DBCC PAGE to pull the last CheckDB run out of SQL Server […]

Leave a comment

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.

January 2013
« Dec   Feb »

Welcome , today is Wednesday, February 19, 2020