• subscribe
August 21, 2002 12:00 AM

Trying to Recover Without an .ldf

SQL Server Pro
InstantDoc ID #26044
Downloads
26044.zip

I'm trying to recover a database that has one master data file (.mdf) and one log data file (.ldf). I got the .mdf file from a standard OS backup tape. But sp_detach_db wasn't run on the database before the .mdf backup, so I don't have the .ldf file. I know that the stored procedure sp_attach_single_file_db can recreate the log file in some cases, and I've tried to use it to simply reattach the database, but I get the following error:

Server: Msg 1813, 
Level 16, State 2, Line 1
Could not open new database 'db'.
 CREATE DATABASE is aborted.
Device activation error. 
The physical file name 
'C:\Program Files\Microsoft SQL ServerMSSQL\datadb_log.LDF' may be incorrect.

Can I recover my database?

SQL Server Books Online (BOL) clearly documents that you must run sp_detach_db on a database to let the database reattach with sp_attach_db or sp_attach_single_file_db. Using sp_detach_db ensures transactional consistency within the database and ensures data integrity. However, if complete data integrity isn't important or you know that no data has changed recently, you might be able to use the undocumented Database Consistency Checker (DBCC) REBUILD_LOG command that Listing 3 shows to attach the database. REBUILD_LOG will recreate a new log file and let you reattach a database even if a good log file doesn't exist. However, the data might not be transactionally consistent because you might have thrown away active and uncommitted transactions. Use this command only for emergency recovery when you move data to a new database.

Use caution when you apply any undocumented technique in a production environment. I strongly encourage you to contact Microsoft Product Support Services (PSS) for recovery of production data rather than use undocumented recovery techniques. But sometimes, tips such as this one are good to have in your bag of tricks.



ARTICLE TOOLS

Comments
  • JAMES
    5 years ago
    Feb 04, 2007

    It would be helpful to have a discussion regarding how to put a database into emergency mode.

  • rakesh
    7 years ago
    Dec 06, 2005

    ) move the existing .MDF file to a new location (to backup)
    2) Start SQL Server Enterprise manager, and create a new (dummy) database where the .MDF file is the exact same name and size of your old/existing .MDF. The ldf file can remain at 1mb in size.

    3) Stop the SQL Server service and copy only the .MDF to the location of the new/dummy .MDF file.

    4) Start SQL Server. At this point, the database should come up as suspect. You will then want to place the database into emergency bypass mode and rebuild the Transaction log. This can be accomplished by performing the following steps:

    a) Change the database context to Master and allow updates to system tables. Note you will be performing this within Query Anayzler:
    Use Master
    go
    sp_configure 'allow updates',1
    reconfigure with override
    go
    b) Set the database in emergency bypass mode:
    Select dbid, name, status from sysdatabases where name = '
    -- Note the value of status and write it down for future reference
    begin tran
    update sysdatabases set status = 32768 where name = '
    commit tran
    c) Stop and restart your SQL Server. At this point, the database will come up in emergency mode. At this point, you will be able to browse, but not update any data within the database.

    d) We will now want to rebuild the log/ldf file. To do this, you will want to run the following:

    DBCC rebuild_log('',')
    If you do not receive any errors, we will want to reset the status of the database by running the following commmand and restarting SQL Server:
    use master
    exec sp_dboption 'database name','single user',true
    go
    begin tran
    update sysdatabases set status = 0 where name = '
    commit tran
    e) set database option to not allow updates to the system tables:

    sp_configure 'allow updates', 0
    reconfigure with override
    go
    f) stop and restart SQL Server
    When the server restarts, you will then want to launch Query Analyzer and run the following to validate the database and check the overa

  • Gurdip
    7 years ago
    Sep 20, 2005

    Hi,
    Can anyone tell me where in the article it mentions how to attach the MDF? Please forgive me im a novice.

  • Anonymous User
    7 years ago
    Sep 02, 2005

    Brian Moran, you are a GOD! I don't know how long i looked around for info on rebuilding the log file. You sir are a true god sent. Thank you thank you thank you!

  • Anonymous User
    7 years ago
    Aug 17, 2005

    What if you have multiple data files, and you are missing one of the ldf files?

You must log on before posting a comment.

Are you a new visitor? Register Here