Run DBCC CHECKTABLE against all the tables in a database

This is a short piece of code that will run DBCC CHECKTABLE against all the tables in a database. 

In case you were wondering, DBCC CHECKDB does the following:

Runs DBCC CHECKALLOC on the database.
Runs DBCC CHECKTABLE on every table and view in the database.
Validates the Service Broker data in the database.
Runs DBCC CHECKCATALOG on the database.
Validates the contents of every indexed view in the database.

and DBCC CHECK TABLE does these tasks:

Index, in-row, LOB, and row-overflow data pages are correctly linked.
Indexes are in their correct sort order.
Pointers are consistent.
The data on each page is reasonable, included computed columns.
Page offsets are reasonable.
Every row in the base table has a matching row in each non-clustered index, and vice-versa.
Every row in a partitioned table or index is in the correct partition.

So, running CHECKDB is more comprehensive.

 

Related Articles

... and you 'll find more on the SQL (General) Menu

DECLARE @tablename VARCHAR(100)
   
DECLARE curName CURSOR LOCAL FAST_FORWARD FOR
    SELECT  table_name
        from INFORMATION_SCHEMA.TABLES t
        WHERE t.TABLE_TYPE = 'base table'
        ORDER BY 1
   
OPEN curName
WHILE 1=1
BEGIN
    FETCH NEXT FROM curName INTO @tablename
    if @@fetch_status <> 0 begin
        break
    end
   
    PRINT @tablename   
    DBCC CHECKTABLE (@tablename) 
END
CLOSE curName
DEALLOCATE curName


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences