MS SQL Server database or table repair (DBCC CHECKDB)

Source: Internet
Author: User

MS SQL Server provides a number of database repair commands that can be tried when the database is questioned or if a read cannot be completed.
1. DBCC CHECKDB
After restarting the server, execute the following SQL in SQL Query Analyzer to repair the database without any action, and fix the consistency errors and allocation errors that exist in the database.


Use master
DECLARE @databasename varchar (255)
Set @databasename = ' Name of the database entity to be repaired '
exec sp_dboption @databasename, n ' single ', n ' true '--to place the target data base in single-user state
DBCC CHECKDB (@databasename, Repair_allow_data_loss)
DBCC CHECKDB (@databasename, Repair_rebuild)
exec sp_dboption @databasename, n ' single ', n ' false '--to set the target data base to multi-user state

Then execute DBCC CHECKDB (' name of the database entity that needs to be repaired ') to check whether the database still has errors. Note: Repair may result in loss of some data.

2. DBCC checktable
If the DBCC CHECKDB check still has errors, you can use DBCC CHECKTABLE to fix them.
Use the name of the database entity that needs to be repaired
DECLARE @dbname varchar (255)
Set @dbname = ' Name of the database to be repaired '
exec sp_dboption @dbname, ' Single user ', ' true '
DBCC CHECKTABLE (' name of the data table to be repaired ', repair_allow_data_loss)
DBCC CHECKTABLE (' name of the data table to be repaired ', repair_rebuild)
------Change the name of the data table that needs to be repaired to the name of the data table in which the DBCC CHECKDB times error was performed
exec sp_dboption @dbname, ' Single user ', ' false '

3. Other Common repair commands
DBCC Dbreindex Rebuilding one or more indexes of a table in a specified database
Usage: DBCC dbreindex (table name, ') fixes all indexes on this table.

===================================

SQL Server database detection and repair method  
with the promotion of K/3 products, customer service personnel are required to understand the SQL Server database more. During the use of K/3, the database files are used frequently, for some reason, the database may be damaged, this article will be a brief explanation of the database detection and repair methods for this situation. I hope you have new discoveries in the actual work process, and provide us with information in time for further updates.  
1.1 SQL Server database detection  
SQL Server provides commands for database detection, which can be used by DBCC CHECKDB to detect the allocation and structure of individual objects in the database, and can be controlled by one parameter. All the error messages are displayed. Its syntax is as follows:  
DBCC checkdb 
(' database_name ' [, NOINDEX | {repair_allow_data_loss 
| repair_fast 
| repair_rebuild 
}] 
) [With {ALL_ERRORMSGS | no_infomsgs}] 
parameter description:  
' database_name ' represents the database entity name being detected;  
Noindex means non-clustered index of non-system table not detected;  
Repair_allow_data_loss | Repair_fast| Repair_rebuild refers to a direct fix found error, where Repair_allow_data_loss represents, if this error cannot be repaired, the system will directly delete the relevant data. When you have any of these three parameters, the database must be in single-user mode, set in the database properties in Enterprise Manager, &NBSP, or the
ALL_ERRORMSGS representative will display the detected error messages, otherwise Displays up to 200 error messages per table,  
No_infomsgs represents a report that hides all information and occupies space.  
is detected, for the wrong object, will be reported as the object ID of the specific error information, according to the object ID to the system table sysobjects to find the related table, that is, name.

1.2 Fix for SQL Server problem database
After the database detection, we can take the corresponding measures to deal with the problems that arise. If there is a problem with the physical storage of the object after detection, the DBCC CHECKALLOC can be used to fix it:
DBCC checkalloc (' database_name ' | Repair_rebuild}]) [With {ALL_ERRORMSGS | NO_INFOMSGS}]
If there is an error indexing non-system objects, the DBCC DBREINDEX can be used to fix them:
DBCC dbreindex ([' Database.owner.table_name ' [, index_name [, FillFactor]]) [with NO_INFOMSGS]
In both cases, you can also use the DBCC CHECKDB (' db_name ', repair_rebuild) to fix it directly.
Another scenario is that the data connection cannot be established when the test is in progress, indicating that the database is corrupted. In this case, we can take the following measures to try to repair.
First, create a new database in SQL Enterprise (such as the database named Test), stop the SQL Server Service Manager after the database is built, and rename the MDF file for the customer database to test _data.mdf ( That is, the primary file name of the new database), and then overwrite the file with the name of the newly created database with the renamed file, and then start SQL Server Service Manager. Set the system table to a change state for the master database
Use Master
Go
sp_configure ' allow updates ', 1
Reconfigure with override
Go
Set the database to a state of emergency:
Update sysdatabases Set status = 32768 where database '
Stop and restart SQL Server Service Manager and rebuild the log file:
DBCC TRACEON (3604)
DBCC rebuild_log (' Test ', ' Test _log_ldf ')
Set the database to single-user mode and then detect:
sp_dboption ' Test ', ' Single user ', ' true '
DBCC CHECKDB (' test ')
Go
When this database executes CHECKDB, the indexes of some tables are found to be corrupted, and the indexes are rebuilt for the specific table:
DBCC dbreindex (table name)
If the above operation is still not resolved, if the index broken table is a temporary table or not a critical table, you can be introduced from the new establish accounts, if the main table, you may be a recent backup to (partial) recovery. If you do not have a backup, you cannot repair it.

1.3 Why is SQL Server database vulnerable to corruption?
Here are some of the reasons Microsoft may cause database corruption and some precautions:
Operation problems, including cold starter, hot-drawn hard disk, delete some database files;
Hardware issues, including disk controller issues;
Operating system issues, including some fatal errors related to the system.

1.4 Precautionary measures:
1, regular/irregular execution of Chkdsk (without parameters) to detect the physical structure of the hard disk and repair some chkdsk reported problems;
2, standing data.

1.5 Application Database Repair Example
DECLARE @databasename varchar (255)
Set @databasename = ' AIS20021224170730 '------Be sure to enter it manually
A fix that allows data loss to occur when there are problems---------performing a general fix
---------Data loss repair requires a single user, exit the middle tier, client, and other SQL modules
---All functions exit, set the database to single user in Query Analyzer master

exec sp_dboption @databasename, n ' single ', n ' true '

-----in Query Analyzer master, repair the database
DBCC CHECKDB (@databasename, Repair_allow_data_loss)
DBCC CHECKDB (@databasename, Repair_rebuild)
------Restoring Database state
exec sp_dboption @databasename, n ' single ', n ' false '


2nd. Repair of database log corruption
Follow the steps below to try to rebuild the database transaction log.

Note: Because the transaction log is missing, the database may have no committed data.

Note: You have to replace the real database name

2.1 Step 1:

Create a new database named the name of the original database.

2.2 Step 2:

Stop SQL Server

2.3 Step 3:

Replace the MDF file of the old database with the appropriate MDF file for the new database and remove the LDF file

2.4 Step 4:

Restart the SQL Server service, and then run the following command:

Use Master

Go

sp_configure ' allow updates ', 1

Reconfigure with override

Go

BEGIN Tran

Update sysdatabases Set status = 32768 where Db_name '

--Verify One row is updated before committing

Commit Tran

2.5 Step 5:

Stop SQL and then restart the SQL Server service, and then run the following command:

DBCC TRACEON (3604)

DBCC rebuild_log (' db_name ', ' C:\mssql7\data\dbxxx_3.LDF ')

Go

2.6 Step 6:

Stop SQL and then restart the SQL Server service, and then run:

Use master

Update sysdatabases SET status = 8 where
Go

sp_configure ' allow updates ', 0

Reconfigure with override

Go

2.7 Step 7:
Run DBCC CHECKDB (DB_NAME) to check the integrity of the database.

3rd General processing of database queries
1. Execute the following SQL (open the switch to modify the system table):
EXEC sp_configure ' allow updates ', 1
RECONFIGURE with OVERRIDE
2. Modify the table in database master: sysdatabases
Change the Status field value to 4
3. Then execute the following sql:
EXEC sp_configure ' allow updates ', 0
RECONFIGURE with OVERRIDE.

Transferred from: Zeng Wei Blog | DBCC CHECKDB database or table repair

Original address: http://www.cnblogs.com/zengwei/archive/2009/09/18/1569376.html

MS SQL Server database or table repair (DBCC CHECKDB)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.