How to create a new database with existing MDF and LDF files

Kaplan, Andrew H 101 Reputation points
2023-11-20T17:13:13.53+00:00

Hello.

I am running SQL Server 2016 with the Studio 2019 release, and I would like to create a new database with its own name, using existing MDF an LDF files. The name of the original database is 'bugtracker', and the name of the new database will be 'bugtrackertest'. The files in question are those restored from a backup, and their names are bugtracker.mdf, and bugtracker.ldf respectively.

I have gone into Studio, and have selected the 'Attach' option under the 'Databases' menu item. The Attach Databases window has appeared on-screen, and I am able to select the corresponding MDF and LDF files. My question is the following: If I want to create the new database as described above, do I modify the name of the database in the 'Attach As' field, or is there more that needs to be done?

SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2023-11-20T19:45:14.5033333+00:00
    USE master;  
    GO  
    CREATE DATABASE bugtrackertest   
        ON (FILENAME = 'C:\DATA\bugtracker.mdf'),  
        (FILENAME = 'C:\DATA\bugtracker_log.ldf')  
        FOR ATTACH;  
    GO 
     
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.