Restore db with 1 .mdf file instead of 2 files.

  • I have a db that has 2 data files. I'd like to restore as one data file.

    What's the best way to do this?

  • What is the file extension you are holding??

    If you have .mdf and .ldf files then you can use sp_attach_db stored procedure to attach the db

    If you have .mdf file alone then you can use sp_attach_single_file_db stored procedure stored procedure to attach the db

  • I have 2 data files...(I'd like the 2 data files to become 1

    Data.mdf

    Data2.ndf

    1 log file

    Log.ldf

  • Try the following command.

    sp_attach_db 'dbname','y:\xxx\Data.mdf','y:\xxx\Data2.ndf','y:\xxx\Log.ldf'

    Before executing change the dbname with correct database name and change the path y:\xxx with the correct path where the file exists.

  • You have 2 data files one with extn ,mdf and other with .ndf. If so then you can combine them into one by doing the following

    1. attach the database

    2. create a copy of the database with out any structures.

    3. move the objects and data from both data files into the new database which stores the data in a songle file

    4. remove the older database

    5. renmae this to be your database

    hope i have clarified your doubt. incase if i have misunderstood you. let me know.

    Cheers,
    Sugeshkumar Rajendran
    SQL Server MVP
    http://sugeshkr.blogspot.com

  • I agree with Sugeesh on this issue.

  • You can also potentially move all the objects from the NDF into the MDF and then remove the NDF from the database. You can move tables by moving the clustered indexes. Sprocs, functions, indexes will probably need to be dropped and recreated specifying the MDF as the location in the ON clause.

  • Easiest way to do this if this is just another file in the same filegroup is to use the emptyfile option of shrinkfile:

    use dbname

    go

    dbcc shrinkfile(data2.ndf,emptyfile)

    alter database whatever remove file logical file name of .ndf

    Backup database before you start!

    may take a while if data file contains a lot of data

    ---------------------------------------------------------------------

  • I should have remembered that. Good idea, George!

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply