1. After DBCCCHECKDB restarts the server and does not perform any operations, run the following SQL statement in the SQL query analyzer to fix Database Consistency errors and allocation errors. Usemasterdeclare @ databasenamevarchar (255) set @ databasename name of the database entity to be repaired exe
1. After dbcc checkdb restarts the server and does not perform any operations, run the following SQL statement in the SQL query analyzer to fix Database Consistency errors and allocation errors. Use master declare @ databasename varchar (255) set @ databasename = 'name of the database entity to be repaired 'exe
1. DBCC CHECKDB
After the server is restarted, if no operation is performed, execute the following SQL statement in the SQL query analyzer to repair the database and fix the consistency and allocation errors of 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' -- set the target database to single-user status
Dbcc checkdb (@ databasename, REPAIR_ALLOW_DATA_LOSS)
Dbcc checkdb (@ databasename, REPAIR_REBUILD)
Exec sp_dboption @ databasename, N 'single ', N 'false' -- set the target database to a multi-user State
Then run dbcc checkdb ('name of the database entity to be repaired ') to check whether the database is still faulty. Note: Some data may be lost after repair.
2. DBCC CHECKTABLE
If dbcc checkdb check still has an error, you can use dbcc checktable to fix it.
Name of the database entity to be repaired in use
Declare @ dbname varchar (255)
Set @ dbname = 'name of the database entity 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 'name of the data table to be repaired name' to the name of the data table reported by dbcc checkdb.
Exec sp_dboption @ dbname, 'single user', 'false'
3. Other common repair commands
Dbcc dbreindex rebuilding one or more indexes of a table in the specified database
Usage: dbcc dbreindex (Table Name, '') fixes all indexes in the table.
==========================================
SQL SERVER database detection and repair methods
With the promotion of K/3 products, customer service personnel are required to learn more about SQL SERVER databases. During K/3 usage, database files are frequently used. For some reason, the database may be damaged, this article will give a brief explanation of the database detection and restoration methods in this case. We hope that you will provide us with information in time for further updates when there are new discoveries in your actual work.
1.1 SQL SERVER database Detection
SQL SERVER provides database detection commands. You can use DBCC CHECKDB to check the allocation and structure correctness of each object in the database. You can use one parameter to control the allocation, display All error messages. The 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' indicates the name of the database entity to be detected;
NOINDEX indicates that non-clustered indexes of non-system tables are not detected;
REPAIR_ALLOW_DATA_LOSS | REPAIR_FAST | REPAIR_REBUILD indicates that the error is directly fixed. In this case, REPAIR_ALLOW_DATA_LOSS indicates that the system will directly Delete the relevant data if the error cannot be fixed. When either of the three parameters is included, the database must be in single-user mode and can be set in the database attributes of Enterprise Manager;
ALL_ERRORMSGS indicates that all detected error messages are displayed. Otherwise, up to 200 error messages are displayed for each table;
NO_INFOMSGS indicates hiding all information and space-consuming reports.
After detection, the error OBJECT will be reported in the form of an object id. You can find the relevant table (NAME) in the system table sysobjects Based on the object id.
1.2 SQL SERVER problem database repair
After database detection, You can take corresponding measures to solve the problems. If a problem occurs in the physical storage of the object after detection, you can use dbcc checkalloc to fix the problem:
Dbcc checkalloc ('database _ name' | REPAIR_REBUILD}]) [WITH {ALL_ERRORMSGS | NO_INFOMSGS}]
If the index of a non-system object fails, you can use dbcc dbreindex to fix it:
Dbcc dbreindex (['database. owner. table_name '[, index_name [, fillfactor]) [WITH NO_INFOMSGS]
You can use dbcc checkdb ('db _ name', repair_rebuild) to fix the preceding two cases.
In another case, the system prompts that a data connection cannot be established during the detection, indicating that the database is damaged. In this case, we can take the following measures to try to fix the problem.
First, create a new database in SQL Enterprise (for example, the database name is test). After the database is created, stop SQL Server Service Manager and rename the MDF file of the customer database to test _ data. mdf (the master file name of the new database), overwrite the file with the same name of the new database with the renamed file, and then start SQL Server Service Manager. Set the system table to changeable status for the Master database
Use Master
Go
Sp_configure 'Allow updates', 1
Reconfigure with override
Go
Set the database to an emergency:
Update sysdatabases set status = 32768 where database'
Stop and restart SQL Server Service Manager and recreate the Log file:
Dbcc traceon (3604)
DBCC REBUILD_LOG ('test', 'test _ log_ldf ')
Set the database to single-user mode and then perform Detection:
Sp_dboption 'test', 'single user', 'true'
Dbcc checkdb ('test ')
Go
When the database executes the CHECKDB operation, it finds that the indexes of some tables are damaged. Therefore, it re-creates the indexes for specific tables:
Dbcc dbreindex (table name)
If the above operations still cannot be solved, if the index damage table is a temporary table or not a key table, you can introduce it from the new ledger. If it is a master table, it may be recovered through recent backups. If there is no backup, it cannot be repaired.
1.3 why is the SQL Server database vulnerable to damage?
The following are some possible causes of database damage provided by Microsoft and some preventive measures:
Operation problems, including cold start machine, hot hard disk pulling, and deletion of some database files;
Hardware problems, including disk controllers;
Operating system problems, including system-related fatal errors.
1.4 preventive measures:
1. regularly or irregularly execute CHKDSK (without parameters) to detect the physical structure of the hard disk and fix problems reported by CHKDSK;
2. Data is often backed up.
1.5 application database repair example
Declare @ databasename varchar (255)
Set @ databasename = 'ais20021224170730 '------ be sure to manually enter
--------- Perform a general fix to allow data loss when there are still problems
--------- The repair of data loss must be performed under a single user. In this case, exit the middle layer, client, and other SQL modules.
--- All functions exit. Set the database as a single user in the query analyzer master.
Exec sp_dboption @ databasename, N 'single ', N 'true'
----- Repair the database in the query analyzer master
Dbcc checkdb (@ databasename, REPAIR_ALLOW_DATA_LOSS)
Dbcc checkdb (@ databasename, REPAIR_REBUILD)
------ Restore database status
Exec sp_dboption @ databasename, N 'single ', N 'false'
Chapter 4 repair of Database Log corruption
Follow these steps to try to recreate the database transaction log.
Note: Due to the loss of transaction logs, the database may have submitted data.
Note: Both must be replaced with 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 corresponding MDF file of the new database, and delete the LDF file.
2.4 Step 4:
Restart the SQL Server service and 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, restart SQL Server, and 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, restart SQL Server, and 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 database integrity.
Chapter 1 General handling of database challenges
1. Execute the following SQL statement (enable the system table modification function ):
EXEC sp_configure 'Allow updates', 1
RECONFIGURE WITH OVERRIDE
2. Modify the table in the Master database: sysdatabases
Change the value of the status field to 4.
3. Execute the following SQL statement:
EXEC sp_configure 'Allow updates', 0
Reconfigure with override.