DBCC CHECKTABLE is a database console command (DBCC) in Microsoft SQL Server that can be used to check the physical and logical integrity of a table in a database. Checking the integrity of a table can be useful for detecting corruption in the table or for troubleshooting issues with the table.
To use DBCC CHECKTABLE, you must specify the name of the database and the name of the table you want to check. You can also specify additional options to control the level of checks performed and the repair actions taken if any issues are found.
Here is an example of how to use DBCC CHECKTABLE to perform a full check of a table and repair any issues found:
-- Perform a full check of the "Customers" table in the "MyDatabase" database
DBCC CHECKTABLE ('Customers');
Many of the parameters are the same as CheckDB including DATA_PURITY and INFO_MSGS
Here is an example of how to use DBCC CHECKTABLE to perform a full check of a table and attempt to repair any issues found with REPAIR_REBUILD option:
-- Perform a full check of the "Customers" table
-- and attempt to repair by rebuilding indexes
DBCC CHECKTABLE ('Customers', REPAIR_REBUILD);
Here is an example of how to use DBCC CHECKTABLE to perform a full check of a table and repair any issues found with REPAIR ALLOW DATA LOSS simply throwing out any pages with damage. This is a very dangerous command to use and WILL result in lost data:
-- Perform a full check of the "Customers" table
-- and repair any issues found by throwing out corrupt pages
DBCC CHECKTABLE ('Customers', REPAIR_ALLOW_DATA_LOSS);
The output of DBCC CHECKTABLE will be a message indicating whether the operation was successful or not. If the operation was successful, and no issues were found with the table, the message “DBCC execution completed. If DBCC printed error messages, contact your system administrator.” will be displayed. If any issues were found with the table, error messages describing the issues will be displayed.
Its important to note that DBCC CHECKTABLE can be a resource-intensive operation, as it requires checking the entire table for issues.
Need help with this or an of the other DBCC commands? The team at Stedman Solutions, LLC specializes in repairing corrupt databases and heping when things go wrong.
More from Stedman Solutions:
Steve and the team at Stedman Solutions are here for all your SQL Server needs.
Contact us today for your free 30 minute consultation..
We are ready to help!