Fix the failure of attaching only the mdf file to the database
The following error is reported:
Server: Message 1813, level 16, status 2, Row 1
Failed to open the new database 'test '. Create database will be terminated.
Device activation error. The physical file name "d: \ data \ test_log.LDF 'may be incorrect.
Steps:
A. Use "Enterprise Manager" to create A new data with the same name. You can modify the default path. For ease of expression, I use D: \ data \, the data file name is test. mdf, and the log file name is test_log.ldf.
B. Stop the SQL Service
C. Delete the test_log.ldf file and replace the new test. mdf file with the old test. mdf file.
D. Start the SQL service. After entering the Enterprise Manager, the test data is displayed as "Suspect". After that, no one is connected to the database and no operation is performed.
E. Set the test database to allow direct operation of system tables. In SQL Server Enterprise Manager, right-click the database Server and select "properties". On the "Server Settings" Page, select "allow direct modification to system directory. You can also use the following statement. I didn't use commands in actual operations. You can test the command method yourself. ^_^
Copy codeThe Code is as follows:
Use master
Go
Sp_configure 'Allow updates', 1
Go
Reconfigure with override
Go
F. Set the test database to the emergency recovery mode. Run the following statement in the query Analyzer: update sysdatabases set status =-32768 where dbid = DB_ID ('test ')
After the code is copied and executed, refresh it in the Enterprise Manager and the ttdb database is displayed as "Read-Only \ suspect \ offline \ emergency mode ". You can see the tables in the database, but only the system tables.
G. Recreate the database log file. Run the following statement in the query Analyzer:
Copy codeThe Code is as follows:
Dbcc rebuild_log ('test', 'd: \ Data \ test_log.ldf ')
Before execution, you must exit the Enterprise Manager and no one is connected to some databases. Otherwise, the following error is reported:
Server: Message 5030, level 16, status 1, Row 1
The database cannot be locked for this operation.
DBCC execution is complete. If DBCC outputs an error message, contact the system administrator.
Normal message:
Warning: the log of the database 'test' has been rebuilt. Transaction consistency is lost. Dbcc checkdb should be run to verify physical consistency. The database must be reset and redundant log files may need to be deleted.
DBCC execution is complete. If DBCC outputs an error message, contact the system administrator.
In SQL Server Enterprise Manager, you can see that the database status is "only for DBO ". You can access the user tables in the database.
H. Verify the Database Consistency (which can be omitted, But I faithfully executed ^_^) and run the following command in the query Analyzer:
Copy codeThe Code is as follows:
Dbcc checkdb ('test ')
After the execution of each table is reported, the following results are reported:
CHECKDB finds 0 allocation errors and 0 consistency errors (in the database 'test ).
DBCC execution is complete. If DBCC outputs an error message, contact the system administrator.
I. Set the database to normal. Execute the statement:
Copy codeThe Code is as follows:
Sp_dboption 'test', 'dbo use only', 'false'
If no error is reported, you can breathe a sigh of relief. : P
J. In the last step, we need to restore the "allow direct modification to the system directory" set in Step E. Of course you don't need to talk about how to do this. You can also execute the following statement:
Copy codeThe Code is as follows:
Sp_configure 'Allow updates', 0
Go
Reconfigure with override
Go