When you add a ms SQL SERVER database, you may encounter errors of 9003 or 9004, which are usually caused by mismatch between log files and data files. It is best to restore the backup file directly with backup. If there is no backup, it is still relatively simple to solve this problem. This method also applies to databases with no log files attached.
The general idea is:
- Create a database with the same name
- Stop SQL service
- Overwrite the MDF file, delete the log file, and then start the SQL service
- Change the database from challenge mode to single-user emergency mode
- Fix DBCC CHECKDB
The following describes the specific operation details. Steps 1st, 2, and 3 do not need to be said. Starting from Step 4, we assume that an error is reported when a database named testdb is attached.
Step 3: overwrite the file. After the SQL service is restarted, the database will be in the "questionable" status. Create a new query and execute the following SQL statement,
USE MASTER
GO
Exec sp_configure 'allow updates', 1
GO
Reconfigure with override
GO
Alter database testdb
SET SINGLE_USER
With rollback immediate;
GO
Alter databas 'testdb' SET EMERGENCY
GO
After the execution is complete, run the following query to restore the database:
Dbcc checkdb ('testdb', REPAIR_ALLOW_DATA_LOSS)
Then execute the following SQL statement to bring the database online.
Alter database testdb
SET MULTI_USER
With rollback immediate;
GO
Alter database testdb SET ONLINE