The story begun with an unexpected server problem, my database crashed, its status changed into ‘SUSPECT’, if we would trace back further, this accident seems to be an impact of the electrical failure.
I remember once in the past, Anastasia guided me how to handle SQLServer, and if there’s a ’suspect’ database there are steps to be done:
1. Turn off the SQL Server’s service
2. Copy and backup .MDF and .LDF file of the ’suspect’ database
3. Turn on the service again
4. Detach the database, this might delete the master .MDF and .LDF files
5. Re-attach the .MDF file using an SQL script: sp_attach_single_file_db. A complete resource can be found in here: http://msdn2.microsoft.com/en-us/library/ms174385.aspx
6. After the .MDF is attached, the .LDF or log file will be created automatically.
The solution can also be applied when your database log files is/are beginning to explode.
But on my case yesterday, that solution didn’t work. Perhaps the solution didn’t work on SQL Server 2005, under MS. Windows 2003 x64.
When i tried this script:
sp_attach_single_file_db [ @dbname= ] 'dbname', [ @physname= ] 'physical_name'
then the result was:
File activation failure. The physical file name “C:\xxxxxx\xxxxxx_log.ldf” may be incorrect.
The log cannot be rebuilt because the database was not cleanly shut down.
Msg 1813, Level 16, State 2, Line 1
Could not open new database ‘XXXXXX’. CREATE DATABASE is aborted.
grmmbll…how can it be, i thought…then i tried another weapon:
sp_attach_db [ @dbname= ] 'dbname' , [ @filename1= ] 'filename_n' [ ,...16 ]
Alas!!..enemies were still alive..
Msg 1813, Level 16, State 2, Line 1Could not open new database ‘XXXXXX’. CREATE DATABASE is aborted.
Msg 3316, Level 21, State 1, Line 1
During undo of a logged operation in database ‘XXXXXX’, an error occurred at log record ID (257:73855:17). The row was not found. Restore the database from a full backup, or repair the database.
I got more headaches, so i used my ultimate missile, which means, using the SQLServer 2005’s wizard:
But there came yet another error:
TITLE: Microsoft SQL Server Management Studio
——————————
Attach database failed for Server ‘QQQQQQQQQ’. (Microsoft.SqlServer.Smo)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00.3042.00&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Attach+database+Server&LinkId=20476
——————————
ADDITIONAL INFORMATION:
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
——————————
During undo of a logged operation in database ‘XXXXXX’, an error occurred at log record ID (257:73855:17). The row was not found. Restore the database from a full backup, or repair the database.
Could not open new database ‘XXXXXX’. CREATE DATABASE is aborted. (Microsoft SQL Server, Error: 3316)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&EvtSrc=MSSQLServer&EvtID=3316&LinkId=20476
——————————
BUTTONS:
OK
——————————
Oh no, so what should i do?..i began to scratch my head as if i got my head itchy..
after lurking around the web, i found some links which potentially have the solutions:
1. http://www.webservertalk.com/archive132-2006-3-1445098.html
2. http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=630182&SiteID=1
3. http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_22465639.html
MOre or less, the solutions are alike:
1. Back up the .mdf/.ndf files at first!!!
2. Change the database context to Master and allow updates to system tables:
Use Master
Go
sp_configure ‘allow updates’, 1
reconfigure with override
Go
3. Set the database in Emergency (bypass recovery) mode:
select * from sysdatabases where name = ‘<db_name>’
– note the value of the status column for later use in # 6
begin tran
update sysdatabases set status = 32768 where name = ‘<db_name>’
– Verify one row is updated before committing
commit tran
4. Stop and restart SQL server.
5. Call DBCC REBUILD_LOG command to rebuild a “blank” log file based on the
suspected db.
The syntax for DBCC REBUILD_LOG is as follows:
DBCC rebuild_log(‘<db_name>’,'<log_filename>’)
where <db_name> is the name of the database and <log_filename> is
the physical path to the new log file, not a logical file name. If you
do not
specify the full path, the new log is created in the Windows NT system
root
directory (by default, this is the Winnt\System32 directory).
6. Set the database in single-user mode and run DBCC CHECKDB to validate
physical consistency:
sp_dboption ‘<db_name>’, ’single user’, ‘true’
DBCC checkdb(‘<db_name>’)
Go
begin tran
update sysdatabases set status = <prior value> where name = ‘<db_name>’
– verify one row is updated before committing
commit tran
Go
7. Turn off the updates to system tables by using:
sp_configure ‘allow updates’, 0
reconfigure with override
Go
A big BUT come…but…it didn’t work too..the server shouted at me…
“Ad hoc updates to system catalogs are not allowed.”
I found that in SQL 2005 we can’t update System Tables directly
(http://www.webservertalk.com/archive132-2006-3-1445098.html)
What should I do then?!

