1. Back up the original database file (test_data.mdf, test_log.ldf ). Then, create a new sqlserver registration in the Enterprise Manager.
If it is on the local machine, runIf no logs are attached to the database, a new database log file is automatically created.
Exec Sp_attach_single_file_db ' AA ' , ' D: \ MSSQL \ data \ test_data.mdf '
2: If the log file is lost or the database file copied from another computer:
A. We use the default method to create a database for recovery (such as test ). It can be created in SQL Server Enterprise Manager.
B. Stop the database server.
C. Delete the log file test_log.ldf of the database you just generated, and overwrite the generated database data file test_data.mdf with the MDF file to be restored.
D. Start the database server. In this case, the database test status is "Suspect ". At this time, you cannot perform any operations on this database.
E. Set the 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.
Use Master
Go
Sp_configure ' Allow updates ' , 1
Go
Reconfigure With Override
Go
F. Set test to emergency repair mode.
UpdateSysdatabasesSetStatus=32768 WhereDbid=Db_id('Test')
At this time, you can see in SQL Server Enterprise Manager that the database is in "read-only, suspicious, offline, and emergency mode" to see the tables in the database, but only the system tables
G. Execute the true recovery operation below to recreate the database log file.
DBCCRebuild_log ('Test','C: \ Program Files \ Microsoft SQL Server \ MSSQL \ data \ test_log.ldf')
If the following prompt is displayed during execution:
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. [Brown] [/I]
Describe your otherProgramThe database is being used. If you opened the system table of the test database using SQL Server Enterprise Manager in step f, you can exit SQL Server Enterprise Manager.
The prompt for correct execution should be similar:
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 ". Now you can access the user tables in the database.
H. Verify Database Consistency (omitted)
DBCCCheckdb ('Test')
The general execution result is as follows:
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
Sp_dboption'Test','DBO use only','False'
If no error occurs, Congratulations. Now we can use the recovered database normally.
J. In the last step, we need to restore the "allow direct modification to the system directory" set in Step E. It is dangerous to directly operate system tables. Of course, we can recover the data in SQL Server Enterprise Manager or use the following statement.
Sp_configure ' Allow updates ' , 0
Go
Reconfigure With Override
Go