How to recover database from MDF in SQL Server 2005?

后端 未结 9 418
野性不改
野性不改 2021-01-31 11:20

I have an MDF file and no LDF files for a database created in MS SQL Server 2005. When I try to attach the MDF file to a different SQL Server, I get the following error messa

相关标签:
9条回答
  • 2021-01-31 11:58

    I found the following document on Experts Exchange.

    patrikt: You will have data loss but it can be done.

    1. Detach database and move your mdf to save location.
    2. Create new databse of same name, same files, same file location and same file size.
    3. Stop SQL server.
    4. Swap mdf file of just created DB to your save one.
    5. Start SQL. DB will go suspect.
    6. ALTER DATABASE yourdb SET EMERGENCY
    7. ALTER DATABASE yourdb SET SINGLE_USER
    8. DBCC CHECKDB (yourdb, REPAIR_ALLOW_DATA_LOSS)
    9. ALTER DATABASE yourdb SET MULTI_USER
    10. ALTER DATABASE yourdb SET ONLINE
    
    0 讨论(0)
  • 2021-01-31 12:01

    FROM a post at SQL Server Forums Attaching MDF without LDF:

    If you want to attach a MDF without LDF you can follow the steps below It is tested and working fine

    1. Create a new database with the same name and same MDF and LDF files

    2. Stop sql server and rename the existing MDF to a new one and copy the original MDF to this location and delete the LDF files.

    3. Start SQL Server

    4. Now your database will be marked suspect 5. Update the sysdatabases to update to Emergency mode. This will not use LOG files in start up

    Sp_configure "allow updates", 1
    go
    Reconfigure with override
    GO
    Update sysdatabases set status = 32768 where name = "BadDbName"
    go
    Sp_configure "allow updates", 0
    go
    Reconfigure with override
    GO
    
    1. Restart sql server. now the database will be in emergency mode

    2. Now execute the undocumented DBCC to create a log file

    DBCC REBUILD_LOG(dbname,'c:\dbname.ldf') -- Undocumented step to create a new log file.

    (replace the dbname and log file name based on ur requirement)

    1. Execute sp_resetstatus

    2. Restart SQL server and see the database is online.

    UPDATE: DBCC REBUILD_LOG does not existing SQL2005 and above. This should work:

    USE [master]
    GO
    CREATE DATABASE [Test] ON 
        (FILENAME = N'C:\MSSQL\Data\Test.mdf')
        FOR ATTACH_REBUILD_LOG
    GO
    
    0 讨论(0)
  • 2021-01-31 12:01

    See here : Rebuild master and restore system databases from complete disk failure which has a very nice explanation

    0 讨论(0)
  • 2021-01-31 12:02

    Just had this problem myself, but none of the above answers worked for me.

    But instead, I found this which worked a treat and so I thought I'd share this for everyone else:

    http://www.kodyaz.com/articles/sql-server-attach-database-mdf-file.aspx

    0 讨论(0)
  • 2021-01-31 12:07

    Here are details that cover parts 2) and 3) in case re-creating log doesn’t work which can happen if MDF file is corrupted.

    You can recover data and structure only by reading MDF file with some third party tool that can de-code what’s written as binary data but even with such tools you can’t always do the job completely.

    In such cases you can try ApexSQL Recover. From what I know this is the only tool that can do this kind of job but it’s quite expensive.

    Much better idea is to try to recover these from any old backups if you have any.

    0 讨论(0)
  • 2021-01-31 12:08

    As stated here

    You need a third-party tool to do this. The SQL recovery tool recovers Tables, Keys, Indexes, Views, Triggers, Stored Procedures, Rules, User Defined Functions, and more. In addition, the MS SQL database recovery tool supports recovery of XML indexes and data types, column set property, sparse columns, and file stream data types.

    0 讨论(0)
提交回复
热议问题