Skip to content

TSQL

DBCC CheckDB All Databases

If you use the SQL Server maintenance plans to run DBCC Check DB, you are not allowed to include TempDB in your DBCC CheckDB sequence. For a while I was using maintenance plans, and ending up adding a separate job step to run TSQL to just check TempDB.

Rather than using two different steps, the regular maintenance plan, and a special step for Temp DB, I have instead dumped the regular maintenance plan step, and just used the following TSQL code to run DBCC CheckDB against all databases on the SQL Server.

Option 1: DBCC CheckDB All Databases using sp_msforeachdb

I have included Option 2 below since there are some pretty serious flaws in the sp_msforeachdb script as shown in option 1, specifically sp_msforeachdb will occasionally skip databases. It also has trouble with databases created with certain characters.  Anyway if you want it, here is, but I would recommend paging down to Option 2 which is much more reliable. I would like to thank Patrick Flynn who showed me the flaws in sp_msforeachdb.

The stored procedure sp_msforeachdb takes a parameter of query that will get run against all databases on your SQL Server Instance.


EXEC sp_msforeachdb 'DBCC CHECKDB(''?'') WITH NO_INFOMSGS;';

You can set it up as a maintenance plan like this:

Read More »DBCC CheckDB All Databases

Week 5 – Winning Solution – Database Corruption Challenge

Find out more about the Week 5 Challenge on the overview blog post.

The winning solution, was submitted by Patrick Flynn, just 3 hours and 18 minutes after the challenge was posted. Patrick provided me with the following steps, and TSQL code to back up the steps:

The process was

(a)    Restore two copies from backup (Old backup has 400 Customers and 1622 Orders)

(b)    Use the supplied MDF, LDF and NDF to hack attach to instance

(c)     Try using tail-log backup but log chain broken also as errors in Boot page (1:9) unable to use all files

(d)    Instead using original backup replace only log file and ndf file (containing user data)

(e)    After switching to emergency mode found errors in Orders table (1:175) but this does not contain new rows

(f)     Able to select the newest 181 rows and insert into original orders table

Final Status was

Orders = 12803 rows

Customers = 400 rows

No corruption remaining

Here is the exact TSQL code that supported that solution.

Read More »Week 5 – Winning Solution – Database Corruption Challenge

An Awesome Day at SQL Saturday Redmond

SQLSat393RedmondBadge

Yesterday was SQL Saturday Redmond. It was a great day, I learned lots, met some great new people, and had a chance to catch up with some old friends. When most people hear about SQL Saturday for the first time, they probably think to themselves “spend all day on a Saturday in technical presentations”, however SQL Saturday is much more than that. The presentations all all great, don’t get me wrong here, but the biggest value that I see is in the people that you get to know, and the things you can learn from the vendors. There were at least 6 people that I know online through Twitter, Fitbit, LinkedIn and other online sites that I had the opportunity to meet in person for the first time this weekend.

SQLSat393FriendsPicture Credit: Thanks Jimmy May (@aspiringgeek)

Read More »An Awesome Day at SQL Saturday Redmond