Restore Database from .MDF and .LDF files in Sql Server 2019

pdsqsql 431 Reputation points
2025-05-13T20:26:29.48+00:00

Hello,

We have Sql Server 2019 and we need to restore the database using .MDF and .LDF file into existing database.

We have received .MDF and .LDF file from another server which is also Sql 2019 and our current server which has already database which is also Sql 2019.

I would like to know what's the best way and how I can restore using .MDF and .LDF file on different server which has already database.

Please keep in mind that Source server which contains .MDF and .LDF files has same Database name which had into Target database name

Source: .MDF and .LDF file

Target Database: SqlDB

  1. I can't use Backup restore?
  2. If I try to use Attach DB method but it should be detached the database into Source first?
  3. Any other method?

Thanks for your help!

SQL Server Transact-SQL
SQL Server Transact-SQL
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Transact-SQL: A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
197 questions
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 121.3K Reputation points MVP Volunteer Moderator
    2025-05-13T20:53:53.1+00:00

    If you have a pair of MDF and LDF files, you need to attach them as:

    CREATE DATABASE thisdb ON (NAME = 'thisdb', FILENAME = '<path>.mdf')
    LOG ON (NAME = 'thisdb_log', FILENAME = '<path>.ldf')
    FOR ATTACH
    

    If the database you want to attach the database under is already taken, you need to do one of:

    1. Rename the other database first.
    2. Use a different name.

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.