Summary

To run DBCC CHECKDB in SQL Server, open a new query in SSMS and run DBCC CHECKDB (‘YourDatabase’) WITH NO_INFOMSGS, ALL_ERRORMSGS. A clean run reports zero allocation and zero consistency errors. Use a cursor to check every database and put it on a SQL Agent schedule. If it finds errors, restore a backup before you ever try a repair.

DBCC CHECKDB is the command that tells you whether a SQL Server database is healthy. Run it on a schedule and it catches corruption early, while you still have good backups to fall back on. Skip it and the first sign of trouble is often a failed query in production.

This guide shows the exact way to run it, the options worth knowing, how to check every database at once and how to read what it gives back. It also covers the safe order to follow if it ever reports a problem.

Key Takeaways

  • Run it with DBCC CHECKDB (‘YourDatabase’) WITH NO_INFOMSGS, ALL_ERRORMSGS in SSMS.
  • NO_INFOMSGS and ALL_ERRORMSGS give clean output that shows only real problems.
  • Check every database with a short cursor script, then put it on a SQL Agent schedule.
  • A clean run reports zero allocation and zero consistency errors. Anything else needs action.
  • If it finds errors, restore a clean backup first. Repair options are a last resort. One of them deletes data.

What does DBCC CHECKDB check?

DBCC CHECKDB checks both the physical and the logical health of everything in a database. Under one command it runs three checks together: it checks how disk space is allocated, it checks the system catalog and it checks every table and index for consistency. That is why it is the one integrity command most DBAs lean on.

It finds the problem. It does not fix anything unless you add a repair option, which is a separate and risky step covered later. For a fuller look at what corruption looks like and how to catch it early, see our guide on checking database corruption in SQL Server.

How to run DBCC CHECKDB in SSMS

The command runs from a query window. There is no wizard to click through, just a short statement you execute against the database you want to check.

Open a new query in SSMS

Open SQL Server Management Studio, connect to your instance, then open a new query window with New Query.

Run the command on your database

Type the command below, swapping in your database name, then press Execute. The two options keep the output clean and complete.

Wait for it to finish, then read the output

On a large database this can take a while. When it ends, read the messages pane for the result.

DBCC CHECKDB ('YourDatabase') WITH NO_INFOMSGS, ALL_ERRORMSGS;

To check the database you are already connected to, you can run DBCC CHECKDB on its own with no name. If the check is taking far longer than you expect, our guide on DBCC CHECKDB taking a long time covers why and how to speed it up.

The options you should know

The options change how thorough the check is and how it runs. These are the ones worth keeping in mind. Microsoft lists the full set in the DBCC CHECKDB reference.

Option What it does
NO_INFOMSGS Hides the informational messages, so you see only errors.
ALL_ERRORMSGS Shows every error per object, not a short capped list.
PHYSICAL_ONLY Skips the logical checks for a much faster run.
EXTENDED_LOGICAL_CHECKS Adds deeper checks on indexed views and XML or spatial indexes.
DATA_PURITY Checks column values for out-of-range or invalid data.
NOINDEX Skips nonclustered index checks, since those can be rebuilt.
TABLOCK Uses locks instead of the internal snapshot.
ESTIMATEONLY Estimates the tempdb space the check will need.
MAXDOP Sets the parallelism for the check, on SQL Server 2016 and later.
REPAIR_REBUILD Repairs with no data loss. A last resort, after a backup.
REPAIR_ALLOW_DATA_LOSS Repairs by deleting corrupt data. Real data loss, last resort only.

How to run it on all databases at once

Checking one database at a time does not scale. This short cursor runs CHECKDB across every online user database on the instance. Drop the database_id filter if you want the system databases included too.

DECLARE @db SYSNAME;
DECLARE db_cursor CURSOR FOR
  SELECT name FROM sys.databases
  WHERE state_desc = 'ONLINE' AND database_id > 4;

OPEN db_cursor;
FETCH NEXT FROM db_cursor INTO @db;
WHILE @@FETCH_STATUS = 0
BEGIN
  DBCC CHECKDB (@db) WITH NO_INFOMSGS, ALL_ERRORMSGS;
  FETCH NEXT FROM db_cursor INTO @db;
END
CLOSE db_cursor;
DEALLOCATE db_cursor;

How to schedule DBCC CHECKDB

A check only helps if it runs on its own, before trouble shows up. Put it on a schedule so you are not relying on memory. There are two easy ways.

The first is a SQL Server Agent job. Paste the script above into a job step, set it to run weekly during a quiet window, then add a job alert so you hear about a failure. The second is a Maintenance Plan with the Check Database Integrity task, which runs CHECKDB for you with a few clicks. Either way, the goal is the same: a regular, hands-off check with an alert when something is wrong.

How to read the results

The result lives in the messages pane, not a pass or fail light. A healthy database ends with a line that reports zero allocation errors and zero consistency errors. That line is what you want to see.

If there are problems, CHECKDB lists each one, names the object and the page it sits on, then ends with the smallest repair level that would clear the errors. That repair line is a description of the damage, not an instruction to run it. So a clean line means you are done, while any error count means it is time to act, in the careful order below.

What to do if it finds errors

Finding errors turns this from a check into a recovery job. The order you follow here decides whether you keep your data or lose part of it.

Restore before you repair

Restore from your most recent clean backup first. It is the safest fix and the one Microsoft recommends over any repair. Only if there is no good backup should you look at the repair options. The heavier of them deletes data to clear the errors.

When the database is badly corrupt, will not come online or has no usable backup, a recovery tool reads the MDF and NDF files directly and pulls the data out, including objects a repair would have deleted. The SQL Database Recovery Software does this on a copy of the file, so the original stays untouched.

Download Now
Purchase Now

Step 1: Load the MDF file

Install and run the tool, then click Open to select and load your MDF file.

Load the MDF file into the SQL recovery tool

Step 2: Pick the scan mode and version

In the Add File window, choose Advanced scan mode, mark your SQL Server version and Recover Deleted Objects, then click OK.

Choose the scan mode and SQL Server version

Step 3: Preview the recovered data

When the scan finishes, preview the recovered tables and records, then click Export.

Preview the recovered SQL Server data

Step 4: Export the data

Set your export options and click Export to write the data to a live database or to scripts.

Export the recovered SQL Server data

Going deeper: repair levels and last-resort options

Most guides stop at “run it and read the output.” The part that decides whether you keep your data is what happens after an error, so this section goes there. It is more technical, so it sits at the end.

REPAIR_REBUILD vs REPAIR_ALLOW_DATA_LOSS

The two repair levels are not two strengths of the same thing. They are different in kind. REPAIR_REBUILD fixes structural problems with no data loss, like a missing nonclustered index row or a broken index link, because that information can be rebuilt from data that is still intact. REPAIR_ALLOW_DATA_LOSS is the destructive one. It clears the errors by deallocating the corrupt pages, rows and all, so the database comes back clean precisely because the bad data is gone. The level CHECKDB prints is the minimum needed to clear every error, not a promise that it is safe.

Always run a repair inside a transaction

Here is a habit that has saved data more than once. Wrap a repair in a transaction so you can see what it did before you accept it. Run BEGIN TRANSACTION, then the repair, then look at what it reported. If you are happy, COMMIT. If it deleted more than you can accept, ROLLBACK and the database is back as it was. The repair needs the database in single-user mode first.

ALTER DATABASE YourDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
BEGIN TRANSACTION;
DBCC CHECKDB ('YourDatabase', REPAIR_ALLOW_DATA_LOSS) WITH NO_INFOMSGS, ALL_ERRORMSGS;
-- review the output, then COMMIT TRANSACTION or ROLLBACK TRANSACTION
ALTER DATABASE YourDatabase SET MULTI_USER;

When the database will not even come online

Sometimes CHECKDB cannot run because the database is marked suspect and will not start. As a last resort you can force it into emergency mode, which lets CHECKDB read pages the engine has marked bad and attempt a repair. This can rebuild the transaction log, which may break transactional consistency, so it really is the final option after the data is saved elsewhere. Microsoft explains the path in its guide to troubleshooting consistency errors.

ALTER DATABASE YourDatabase SET EMERGENCY;
ALTER DATABASE YourDatabase SET SINGLE_USER;
DBCC CHECKDB ('YourDatabase', REPAIR_ALLOW_DATA_LOSS) WITH NO_INFOMSGS, ALL_ERRORMSGS;
ALTER DATABASE YourDatabase SET MULTI_USER;

Read the error like a map

Every consistency error is a location, not just a complaint. CHECKDB names the object and the page, with the page shown as file and page number, like 1:24968. The object is given as an ID, which you turn into a table name with a quick lookup. That tells you exactly what is damaged and where, which is far more useful than the summary count alone.

SELECT OBJECT_NAME(<object_id>) AS TableName;
From our experience

The first thing we do with an error list is resolve the object IDs to table names and note the pages. It turns a wall of red text into a short list of which tables are hit and how hard. One detail to watch: errors that show up on one run and vanish on the next are usually not the database. That pattern points at the storage or I/O path. Chasing it inside SQL Server wastes the night.

The safest path keeps the original file

Whatever route you take, protect the original. Before any repair, copy the MDF and NDF files somewhere safe and work only on the copy, so a repair that deletes too much is never the last word. For how a recovery tool reads that MDF directly, page by page, without SQL Server, see the technical section in our guide on DBCC CHECKDB taking a long time.

From our experience

The DBAs who never lose data in a corruption event all share one habit. They treat the live file as evidence and never repair it in place. Copy first, prove you have the data out, then decide what to do with the original. Run REPAIR_ALLOW_DATA_LOSS on the only copy you have and the rows it deletes are gone for good.

Final word

So running DBCC CHECKDB is the easy part. Open a query, run the command with NO_INFOMSGS and ALL_ERRORMSGS, check every database on a schedule and read the messages for that clean zero-errors line. The skill is in what you do next. If it ever reports a problem, restore a backup before you reach for a repair. Never work on your only copy of the file.

Before your next maintenance window, ask the question that matters most. If CHECKDB found corruption tomorrow, do you have a backup you trust? Or would that file be the only copy you have?

Frequently Asked Questions

How do I run DBCC CHECKDB in SQL Server?

Open a new query in SSMS and run DBCC CHECKDB (‘YourDatabase’) WITH NO_INFOMSGS, ALL_ERRORMSGS, then press Execute. To check the database you are connected to, run DBCC CHECKDB on its own with no name.

Which DBCC CHECKDB options should I use?

For a normal check, NO_INFOMSGS and ALL_ERRORMSGS give clean, complete output. PHYSICAL_ONLY makes a large database far faster. Keep the REPAIR options for a last resort, since one of them deletes data.

Can I run DBCC CHECKDB on all databases at once?

Yes. A short cursor over sys.databases runs the check on every online database. Filter database_id greater than 4 for user databases only or drop the filter to include the system databases too.

Does DBCC CHECKDB take the database offline?

No. Since SQL Server 2005 it runs against a hidden internal snapshot, so users keep working. It still uses disk and CPU, so run it during a quiet window on busy servers.

What is the difference between REPAIR_REBUILD and REPAIR_ALLOW_DATA_LOSS?

REPAIR_REBUILD fixes structural issues with no data loss, like index problems. REPAIR_ALLOW_DATA_LOSS clears errors by deleting the corrupt pages, so you lose data. Restore a backup before using either one.

How often should I run DBCC CHECKDB?

Weekly is a common baseline, with a daily PHYSICAL_ONLY check on large or critical databases. Run it often enough that any corruption is caught while you still have a clean backup to restore.