DBCC CHECKDB checks a DB for damage. Here is how I run it, read the result and schedule it, and what I do when it finds a problem.
I ran these scripts on SQL Server 2022. They should work on any supported SQL Server version.
Run DBCC CHECKDB on one DB
DBCC CHECKDB checks every object in a DB for physical and logical damage and reports what it finds. Run it from a query window in SSMS or from sqlcmd. You must be in the sysadmin server role or the db_owner DB role, as the Microsoft DBCC CHECKDB reference states.
DBCC CHECKDB (N'YourDatabase') WITH NO_INFOMSGS;On a clean DB this prints nothing, and SSMS shows "Commands completed successfully." That silence is the sign it worked. Without NO_INFOMSGS, the same run prints a line for every table, system tables included, and ends with a summary line.
What DBCC CHECKDB checks
DBCC CHECKDB runs three smaller commands for you:
- DBCC CHECKALLOC checks how disk space is allocated.
- DBCC CHECKTABLE checks every table and view.
- DBCC CHECKCATALOG checks the system metadata.

It checks Service Broker data too. For FILESTREAM columns, it checks the link between table rows and files. You never need to run the three separately.
It does not check everything. My old notes list two commands it never runs for you, and there are two more limits:
- It does not check identity values. That is DBCC CHECKIDENT.
- It does not check constraints. That is DBCC CHECKCONSTRAINTS.
- It does not check disabled indexes.
- On a DB with memory-optimized tables, it checks only the disk-based tables.
It reports problems. It fixes nothing unless you ask for a repair option.
Pick the options that fit your DB
Use PHYSICAL_ONLY for the frequent check and a full run for the periodic one. These are the options you will meet most often. The Microsoft DBCC CHECKDB reference has full detail:
| Option | What it does | When to use it |
|---|---|---|
PHYSICAL_ONLY | Checks page and record headers and allocation only. It still catches torn pages, checksum failures and common hardware faults. It skips column-value checks and FILESTREAM data. | The frequent check on a large production DB |
NO_INFOMSGS | Hides the informational lines, so only errors and the closing summary appear. | Every scheduled run |
ALL_ERRORMSGS | Microsoft documents that every error is shown by default, so this option changes nothing. | Never needed. Every one of my old scripts has it, from before SQL Server 2008 SP1 |
EXTENDED_LOGICAL_CHECKS | Adds logical checks on indexed views, XML indexes and spatial indexes. Needs compatibility level 100 or higher. Microsoft warns it can slow the run a lot and that you cannot track its progress. | Only if you suspect index problems that are not physical corruption |
MAXDOP = n | Sets the degree of parallelism for this run, even above the instance setting. Available from SQL Server 2014 SP2. | Cap CPU use on a busy server |
-- Frequent check on a large database
DBCC CHECKDB (N'YourDatabase') WITH PHYSICAL_ONLY;
-- Full check, errors only, at most 4 CPUs
DBCC CHECKDB (N'YourDatabase') WITH NO_INFOMSGS, MAXDOP = 4;
-- Add the extra logical checks
DBCC CHECKDB (N'YourDatabase') WITH NO_INFOMSGS, EXTENDED_LOGICAL_CHECKS;Microsoft says PHYSICAL_ONLY implies NO_INFOMSGS. On my test DB, it still printed the closing "CHECKDB found 0 allocation errors" line. The two fit together: the per-table lines go away and the summary stays.
Know the cost before you run it
DBCC CHECKDB reads every page. It competes with your workload for I/O and CPU, but it does not block your users. It works from an internal snapshot of the DB. SQL Server creates a sparse file beside each data file. The file is named like YourDatabase.mdf_MSSQL_DBCC11. SQL Server deletes it when the check ends. The sparse file grows with every page your workload changes during the run. Keep free space on the data drive, and keep index rebuilds and large loads out of the same window.
To see the tempdb space a check needs before you run it, add ESTIMATEONLY. It runs no check.
DBCC CHECKDB (N'YourDatabase') WITH ESTIMATEONLY;My 20,000-row test DB needed about 6.5 MB of tempdb. A large DB needs far more, so run this first on a server with a small tempdb.
A check on a big DB can run for hours. To see how far along it is, run this from another window. I have kept this query in my notes since 2015:
SELECT session_id,
percent_complete,
DATEADD(ms, estimated_completion_time, GETDATE()) AS estimated_end_time,
start_time,
command
FROM sys.dm_exec_requests
WHERE command LIKE N'DBCC%';The advice changes in two cases. TABLOCK skips the snapshot and takes locks instead. It runs faster on a heavily loaded DB, but it blocks writers. It skips the DBCC CHECKCATALOG and Service Broker checks. Tempdb itself gets no snapshot, so a check on tempdb skips the allocation and catalog checks.
Read the output
Read the last two lines first. They give the error counts and, if there are errors, the minimum repair level. A clean run ends like this. My test DB is called t_dbcc_checkdb:
CHECKDB found 0 allocation errors and 0 consistency errors in database 't_dbcc_checkdb'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.To see a damaged result, I changed eight bytes on one page of a nonclustered index in a test DB's data file. The DB was offline. Do not do this to a DB you care about. DBCC CHECKDB then returned four errors for that single page. This is the output, trimmed:
Msg 8939, Level 16, State 98 ... Table error: Object ID 901578250, index ID 2, ... page (1:1016). Test (IS_OFF (BUF_IOERR, pBUF->bstat)) failed. Values are 133129 and -4.
Msg 8928, Level 16, State 1 ... Page (1:1016) could not be processed. See other errors for details.
Msg 8976, Level 16, State 1 ... Page (1:1016) was not seen in the scan although its parent (1:1008) and previous (1:992) refer to it.
Msg 8978, Level 16, State 1 ... Page (1:1017) is missing a reference from previous page (1:1016). Possible chain linkage problem.
CHECKDB found 0 allocation errors and 4 consistency errors in table 'Orders' (object ID 901578250).
CHECKDB found 0 allocation errors and 4 consistency errors in database 't_dbcc_checkdb'.
repair_allow_data_loss is the minimum repair level for the errors found by DBCC CHECKDB (t_dbcc_checkdb).The parts that matter:
page (1:1016)means file 1, page 1016. The four messages all point at that one page.index ID 2is a nonclustered index. Index ID 1 is the clustered index, and ID 0 is a heap. So ID 1 or 0 means the table's own data is damaged.- The last line names the least destructive repair option that can clear the errors. It does not tell you to run it. The next two sections explain why you should not, at least not yet.
SQL Server also records a bad page where you can query it. A query that reads the damaged page fails with error 824 and closes your connection (severity 24). The page is listed in msdb.dbo.suspect_pages. There, event type 2 means a bad checksum:
SELECT DB_NAME(database_id) AS database_name, file_id, page_id, event_type, error_count, last_update_date
FROM msdb.dbo.suspect_pages;database_name file_id page_id event_type error_count
t_dbcc_checkdb 1 1016 2 4SQL Server writes and checks that page checksum only when the DB uses PAGE_VERIFY CHECKSUM. If you are not sure yours does, read our post on PAGE_VERIFY CHECKSUM in SQL Server.
Two more places show whether checks run at all. This script lists the time of the last clean check for every DB. Then it searches the SQL Server error log for the line each run writes:
SELECT d.name,
DATABASEPROPERTYEX(d.name, 'LastGoodCheckDbTime') AS last_good_checkdb
FROM sys.databases AS d
WHERE d.state_desc = N'ONLINE'
ORDER BY last_good_checkdb;
EXEC sys.sp_readerrorlog 0, 1, N'DBCC CHECKDB', N'found';A DB that has never had a clean check shows 1900-01-01 00:00:00.000 (DATABASEPROPERTYEX, SQL Server 2016 SP2, 2017 CU9 and later). On my test instance every DB except the one I had just checked showed that date. The error log line reads "DBCC CHECKDB (t_dbcc_checkdb) executed by sa found 0 errors and repaired 0 errors."
Choose how often to run it
Run a full DBCC CHECKDB at least weekly. Keep backups for longer than the gap between checks. Microsoft recommends PHYSICAL_ONLY for frequent use and a full run now and then. It says the right frequency depends on your business. Our minimum is weekly. A DB that changes fast or holds critical data can be checked daily. The sign you got it right is that LastGoodCheckDbTime is within the last seven days for every DB that can be written to. A read-only DB never updates that date, even after a clean check, so judge it by the check's own result.
Retention (how long you keep backups) matters because a check only helps while a clean backup still exists. Suppose you check monthly but keep only seven days of backups. Damage that started on day 3 may sit in every backup you hold by day 30, when the check finds it. A weekly check with at least two weeks of backups normally leaves you a clean backup to restore.
The advice changes for very large DBs. If a full check does not finish inside your window, run PHYSICAL_ONLY nightly. PHYSICAL_ONLY also updates LastGoodCheckDbTime, so that date alone does not tell you a full check ran. Run the full check against a restored copy on another server. Our post on restoring a backup to a spare server and running DBCC CHECKDB shows why that restore doubles as a backup test.

Not sure whether your DBs have ever had a clean check? Our Health Check runs 145 read-only checks, and DB integrity is one of them. See what our SQL Health Check covers.
Schedule it
Schedule DBCC CHECKDB as a SQL Server Agent job that fails when any DB is not clean. You have three routes:
- Ola Hallengren's DatabaseIntegrityCheck, part of his free maintenance scripts.
- Your own Agent job, like the script below.
- The Check Database Integrity task in a maintenance plan. That task runs DBCC CHECKDB.
On most servers, I just run Ola Hallengren's scripts. They run DBCC CHECKDB on every DB on a schedule, and that covers the day-to-day job. Most of the other commands in this post you only need when a check finds a problem.
If you want your own job instead, the script below is the second route. It checks every online DB except tempdb, and it skips DB snapshots. After each check, it looks at the error code and compares LastGoodCheckDbTime with the time the loop started. Read-only DBs are judged by the error code alone, because their date never moves. If a DB did not finish clean, the loop raises an error that names it.
DECLARE @started datetime = GETDATE();
DECLARE @db sysname, @err int, @failed nvarchar(max) = N'';
DECLARE dbs CURSOR LOCAL FAST_FORWARD FOR
SELECT name
FROM sys.databases
WHERE state_desc = N'ONLINE'
AND database_id <> 2 -- tempdb
AND source_database_id IS NULL -- skip database snapshots
ORDER BY name;
OPEN dbs;
FETCH NEXT FROM dbs INTO @db;
WHILE @@FETCH_STATUS = 0
BEGIN
DBCC CHECKDB (@db) WITH NO_INFOMSGS;
SET @err = @@ERROR;
-- Read-only DBs never update LastGoodCheckDbTime, so judge them by the error code alone.
IF @err <> 0
OR (DATABASEPROPERTYEX(@db, 'Updateability') = N'READ_WRITE'
AND ISNULL(CONVERT(datetime, DATABASEPROPERTYEX(@db, 'LastGoodCheckDbTime')), 0) < @started)
SET @failed += N' ' + QUOTENAME(@db);
FETCH NEXT FROM dbs INTO @db;
END
CLOSE dbs;
DEALLOCATE dbs;
IF @failed <> N''
RAISERROR (N'CHECKDB did not finish clean for:%s', 16, 1, @failed);I tested this loop with one extra line that limited it to my two test DBs, one of them read-only. On the clean copies it printed nothing. On the damaged copy it printed the DBCC errors, then this. It printed the same line when I set the damaged DB to read-only:
Msg 50000, Level 16, State 1, Line 32
CHECKDB did not finish clean for: [t_dbcc_checkdb]This script creates a weekly Agent job that runs the same loop every Sunday at 02:00:
USE msdb;
GO
DECLARE @cmd nvarchar(max) = N'
DECLARE @started datetime = GETDATE();
DECLARE @db sysname, @err int, @failed nvarchar(max) = N'''';
DECLARE dbs CURSOR LOCAL FAST_FORWARD FOR
SELECT name
FROM sys.databases
WHERE state_desc = N''ONLINE''
AND database_id <> 2
AND source_database_id IS NULL
ORDER BY name;
OPEN dbs;
FETCH NEXT FROM dbs INTO @db;
WHILE @@FETCH_STATUS = 0
BEGIN
DBCC CHECKDB (@db) WITH NO_INFOMSGS;
SET @err = @@ERROR;
IF @err <> 0
OR (DATABASEPROPERTYEX(@db, ''Updateability'') = N''READ_WRITE''
AND ISNULL(CONVERT(datetime, DATABASEPROPERTYEX(@db, ''LastGoodCheckDbTime'')), 0) < @started)
SET @failed += N'' '' + QUOTENAME(@db);
FETCH NEXT FROM dbs INTO @db;
END
CLOSE dbs;
DEALLOCATE dbs;
IF @failed <> N''''
RAISERROR (N''CHECKDB did not finish clean for:%s'', 16, 1, @failed);';
EXEC dbo.sp_add_job
@job_name = N'Weekly DBCC CHECKDB',
@description = N'Full CHECKDB of every online database except tempdb. Fails if any database is not clean.';
EXEC dbo.sp_add_jobstep
@job_name = N'Weekly DBCC CHECKDB',
@step_name = N'CHECKDB all databases',
@subsystem = N'TSQL',
@database_name = N'master',
@command = @cmd;
EXEC dbo.sp_add_jobschedule
@job_name = N'Weekly DBCC CHECKDB',
@name = N'Sunday 02:00',
@freq_type = 8, -- weekly
@freq_interval = 1, -- Sunday
@freq_recurrence_factor = 1, -- every week
@active_start_time = 020000; -- 02:00:00
EXEC dbo.sp_add_jobserver @job_name = N'Weekly DBCC CHECKDB';
GOSet the job to notify an operator when it fails. Without a notification, a failed run sits unread in the job history.
What I did not test. I created this job and checked that SQL Server saved it correctly: one step and a weekly schedule at 2:00 AM. I did not wait for the schedule to run it. I also did not test the maintenance plan task, Ola Hallengren's scripts or availability group secondaries.
Restore first when DBCC CHECKDB finds errors
Restore from the last good backup. Microsoft names that as the primary way to recover from DBCC CHECKDB errors. Before you restore, look at which index the errors name. A damaged nonclustered index (index ID 2 or higher) holds only a copy of table data. So you can often rebuild it without losing a row.

In my test, ALTER INDEX ... REBUILD failed with error 824. A rebuild reads the damaged page. When I dropped the index and created it again, the fix worked:
DROP INDEX IX_Orders_CustomerId ON dbo.Orders;
CREATE INDEX IX_Orders_CustomerId ON dbo.Orders (CustomerId);DBCC CHECKDB then ran clean and all 20,000 rows were still there. If the index enforces a primary key or unique constraint, DROP INDEX fails. Drop and add the constraint with ALTER TABLE instead. The advice changes when the errors name index ID 1 or 0. That is the table's own data, and a rebuild cannot recreate it. Restore the backup instead. To start the next test from a clean copy, I restored a backup I took before the damage, and all 20,000 rows came back.
Check the backup before you rely on it. Run RESTORE VERIFYONLY FROM DISK = N'...' WITH CHECKSUM, or better, restore it to another server and run DBCC CHECKDB there. Don't read IsDamaged in RESTORE HEADERONLY as a backup test. A 1 there means the DB was already damaged when the backup ran and the backup carried on past the errors.
If you have no backup that restores, read about recovering a corrupted DB without backups before you try a repair.
Use REPAIR_ALLOW_DATA_LOSS only as a last resort
REPAIR_ALLOW_DATA_LOSS fixes errors by deleting what it cannot repair. Use it only when no backup will restore. Microsoft says it can lose more data than a restore from your last good backup. It also needs single-user mode. Go in this order:
- Run DBCC CHECKDB without a repair option, as above, to learn the minimum repair level.
- Before any repair, take a backup, or stop SQL Server and copy the .mdf, .ndf and .ldf files. A repair cannot be undone.
- If that level is REPAIR_REBUILD, use it. It makes only repairs with no risk of data loss, such as rebuilding an index.
- Use REPAIR_ALLOW_DATA_LOSS only when nothing else works.
REPAIR_FAST does nothing and exists for backward compatibility.
A repair also takes longer than a check. A note I left myself in 2004 says CHECKDB on one DB took 12 minutes, and REPAIR_ALLOW_DATA_LOSS took 28.
If CHECKDB reports damage and your last backup will not restore either, a senior DBA at Red9 starts diagnosing within 15 minutes, at any hour. Talk to SQL Server Emergency Support.
For the test, I damaged a data page of the clustered index and ran the repair:
USE master;
ALTER DATABASE YourDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
DBCC CHECKDB (N'YourDatabase', REPAIR_ALLOW_DATA_LOSS) WITH NO_INFOMSGS;
GO
ALTER DATABASE YourDatabase SET MULTI_USER;
GOThe output said the clustered index was rebuilt and that page (1:450) "has been deallocated." It ended with the line "CHECKDB fixed 0 allocation errors and 4 consistency errors in database 't_dbcc_checkdb'." Then I counted the rows and checked the DB again. Count rows before and after any repair. That note is in my scripts from 2004, and it still applies:
USE YourDatabase;
SELECT COUNT(*) AS rows_left FROM dbo.Orders;
DBCC CHECKDB (N'YourDatabase') WITH NO_INFOMSGS;
DBCC CHECKCONSTRAINTS WITH ALL_CONSTRAINTS, NO_INFOMSGS;The table held 19,966 of its 20,000 rows. The repair had deleted order IDs 3367 through 3400, which is 34 rows. Both checks came back clean. A clean result after a repair does not mean nothing was lost. Microsoft recommends running DBCC CHECKCONSTRAINTS after any repair, because a repair ignores your constraints. It also recommends a full backup right away. And it suggests running a repair inside a transaction, so you can roll it back. I did not test that wrapper.
Run the last-good-check query from the output section on every instance today. A DB you can write to that shows 1900-01-01 has never had a clean check. For it, start with the one-line command at the top of this post.


