Free Information Technology Magazines and eBooks

Wednesday, July 05, 2006

SQL Server Problem: Cannot attach SQL MDF file due to corrupt or missing Log file (Error 1813)

I thought today would be a relaxing day at work. I have no pending jobs on my task list and I was planning to go home early. Upon arriving at the office at 8:00 am the biggest challenge for the day welcomes me with a bang. Our Time and Attendance was down since 5 am in the morning so employees cannot time-in using the biometrics. My colleague reported that the cause was a corrupted transaction log. The transaction log ate the 80G hardisk space which was not normal, so he tried to detach the database, delete the log and rebuild it. Unfortunately the mdf file cannot be re-attach anymore. It prompts the following error:

"Error 1813: Could not open new database '. CREATE DATABASE is aborted.
Device activation error. The physical file name 'C:\ ...\db_name.LDF' may be
incorrect."


After a long research for answer we finally arrived in to a solution. Here are the step-by-step procedure we created to resolve the problem:

1. Created a new database with same MDF and LDF name
2. Stop sql server and delete the new MDF file and copy the old MDF file
3. Started SQL Server and verify the status of the database. If the database status is suspect do the following scripts in Query Analyzer:

a. Override updates in Master Table
Use Master
Go
sp_configure 'allow updates', 1
reconfigure with override
Go

b. Get the value of the status column in sysdatabase table
select * from sysdatabases where name = 'db_name'
-- note the value of the status column for later use in # 6
begin tran


c. Set status of the database to suspect (327768)
update sysdatabases set status = 32768 where name ='db_name'
commit tran


d. Rebuild transaction log
DBCC rebuild_log('db_name','db_log_path')



e. Set the database to single user access
sp_dboption '', 'single user', 'true'
DBCC checkdb('db_name')
Go



f. Restore the status of the database in sysdatabases table
begin tran
update sysdatabases set status = previous_value where name = 'db_name'
-- verify one row is updated before committing
commit tran
Go


g. Restore the override property of Master table
sp_configure 'allow updates', 0
reconfigure with override
Go

4. Now open or refresh the enterprise manager you can now see the tables inside the fixed database.

If any problems persist, you can import all tables into temporary database and drop the original database. Create a new database with same name then re-import all the tables from the temporary database

19 comments:

Anonymous said...

Wow nice work! I will bookmark this solution for future reference

Anonymous said...

Hey Fryan, you are the best!!!!!
You save my ass.
Thanks,
Enzo

Anonymous said...

Thanks for this - excellent solution

You do need to change the db_log_path

Scott said...

Thanks so much!

Scott said...

Me again... I couldn't find your email to send this to you so I thought I'd post a link for anyone in the future. I took your instructions and made them a bit easier. In this text file just follow the directions at the top. Makes it uber easy.

P.S. - Couldn't help but noticing in your profile you're 27 and a Virgo.. me too ;)

nate said...

Very nice. This makes salvaging another hard drive unnecessary!

I got an error rebuilding the log (since it's already there), but I skipped that step and my DB's came up ok!

Thanks a TON!

Chris said...

Thanks a million!!! It took a lot of searching to find this but it was well worth it.

Anonymous said...

Great work!

Thank you.

Anonymous said...

It seems a great solution to the problem. But don't try it if you run SQL Server 2005. It won't let you run the sp_configure.

Anonymous said...

I´m having this error:
[Microsoft][ODBC SQL Server Driver][Shared Memory]ConnectionCheckForData (CheckforData()).
Server: Msg 11, Level 16, State 1, Line 0
Erro geral de rede. Verifique a documentação da rede.
ODBC: Msg 0, Level 16, State 1
Falha de vínculo de comunicação

Connection Broken
Can you help me?
Thanks!

M Oberhardt said...

Thanks heaps. I knew there must have been a way to do this, but had never found it prior.

Masood Ahmad Shah said...

We have restored database using this method on Sql Server 2005.

CREATE DATABASE [JAHIL] ON
( FILENAME = N'C:\SqlData\JAHIL_Data.MDF' ),
( FILENAME = N'C:\SqlData\JAHIL_Log.LDF' )
FOR ATTACH_REBUILD_LOG

it worked like charm and took almost 2 hours for 140 GB of LDF and 15 GB MDF

Anonymous said...

I ran into this problem last night and was able to restore my databases on a SQL Server 2005 install by following these steps:

1. Move mdf and ndf files to another directory (Data_old)
2. Create a database with the same name and same file names and locations as the original databases. (this only applies to the mdf and ndf files the log file can go anywhere)
3. Stop the SQL Server service.
4. Overwrite new mdf and ndf files with the original ones.
5. Start SQL Server.
6. Run this script (Set the @DB variable to the name of your database before running):

Declare @DB sysname;
set @DB = 'DBName';
-- Put the database in emergency mode
EXEC('ALTER DATABASE [' + @DB + '] SET EMERGENCY');
-- Set single user mode
exec sp_dboption @DB, 'single user', 'TRUE';
-- Repair database
DBCC checkdb (@DB, repair_allow_data_loss);
-- Turn off single user mode
exec sp_dboption @DB, 'single user', 'FALSE';

I got an error stating that the log file did not match the data file. You can ignore this as we are rebuilding the log file.

Vaughn Legg said...

Many thanks for your instructions, worked a treat.

Anonymous said...

You are the guy.

Many thanks.

Marcelo
from BRZ.

Anonymous said...

Thank You! Thank You! Thank You! Thank You!

I can't say it enough!

Anonymous said...

I've not tried this but it might solve my problem. Thing is the database I'm trying to attach has 2physical .mdf files. Will your solution still work? If not is there anything else that can be done?

LakiPolitisMediaDesign said...

I can't even begin to describe how thankful I am that I found this article. Absolutely genius!! Seriously appreciated.

I was just about to start banging my head in to the wall, because I couldn't reattach a CMS database I've been working on for about a week after detaching it.

Thanks,

Laki Politis
Laki Politis Media & Design

Anonymous said...

Worked like a charm. I thank you for this. Saved my neck.