Home » Blog » SQL Server » How to Extract Data From MDF File in SQL Server Database? – Manual Tips
SQL Server

How to Extract Data From MDF File in SQL Server Database? – Manual Tips

Ugra Narayan Pandey | Modified: 2022-04-15T15:50:07+05:30 | 5 Minutes Reading
SQL Server

The MDF is a primary database file of SQL Server. So, it’s important to keep its data safe & secure in case of disaster. MDF file can be inaccessible if the associated .ldf file gets missed or lost due to any reason. However, sometimes users want to extract data from SQL MDF file. Therefore, in today’s blog, we will discuss the best and 100% working solution to extract data from MDF file in SQL Server Database. Here, we will also learn how to extract data from corrupt MDF file using SQL recovery software which helps to Recover SQL Database MDF files with all elements.

Download Purchase Now

Table of Content

Introduction of SQL Database Files

SQL Server is a well-known relational database management system that supports three types of database files, such as MDF, NDF, and LDF. The MDF (primary database file) is used to store information like tables, views, stored procedures, and more. The LDF file stores transaction information of the associated primary database file (.mdf). And the NDF (secondary database file) is an optional database file that used to store data physically.

Read More- How to Recover Deleted Data From Table in SQL Server

Solution to Extract Data From MDF File in SQL Server Database

There are two methods that are useful for extracting data from the SQL Primary database file. First with SQL Server Management Studio and second with T-SQL. The next section introduces both methods. You can easily choose one of them.

#Method 1: Using Transact-SQL

Run this command to retrieve data from MDF file:


(FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\Mydatbase.mdf')



This command attach the database to SQL Server even the .ldf file is missing to the associated MDF file. The ATTACH_REBUILD_LOG creates a new transaction log file and a new database.

#Method 2: Using SQL Server Management Studio

Step 1. Open SQL Server Management Studio and connect to the SQL Server instance of Database Engine

Step 2. In Object Explorer, select the Databases and right-click on Attach.

Attach MDF File

Step 3. In the Attach Databases wizard, click on Add button to locate .mdf file you want to attach.

Add MDF File

Step 4. Next, go to the directory where you saved the .mdf file and select the .mdf file. Click OK.

Locate Database Files

Step 5. Now, again click the OK button in the Attach Databases window.

Extract Data From MDF File

Points to Remember to Extract Data from MDF File

  • Make sure the database file is not read-only. For this, you can go to the location of the .mdf file, right-click the .mdf file, click Properties and verify the attribute.
  • Verify that the database file is a higher version and try to attach it in the lower version.
  • The MDF file that you want to attach has the full authority or full access control.

How to Extract Data From Corrupt MDF File?

You can retrieve data from the MDF file which is in a healthy state. However, the method above does not work with damaged MDF files. In such a situation, you need to take the help of SQL Database Recovery software which helps you to recover corrupt MDF file with all components like views, tables, functions, stored procedures, etc. It can recover MDF file of SQL Server 2019, 2017, 2016, 2014, 2012, etc.

Free Download SQL Recovery Tool

This application offers the facility to scan and preview all database items. All the deleted components display in red color. With this tool, one can easily repair and extract data from MDF file. It provides an option to restore MDF files to a new database or an existing database without loss of data. In addition to this, users can also export the recover data to live SQL server environment, SQL Server Compatible SQL Scripts, or CSV File Format.

Step by Step Process to Extract Data from Corrupt MDF File

Step 1. Download, install and run the software. Click on the Open to load .mdf file

Open Database File

Step 2. Choose the Quick or Advance Scan Mode and select the version of the SQL .mdf file. Users can also mark the Recover Delete Objects option to restore the deleted items.

Select SQL Server .mdf file version

Step 3. The scanning process will start. After scanning, you can see the preview of recovered data stored in the MDF file. (All the deleted data are shown in red color).

Preview Database Objects

Step 4. Export data to SQL Server Database, SQL Compatible Scripts, CSV File Format according to your needs.

Restore SQL Database

Step 5. In the end, click the Export button to finalize the process.

Extract Data from Corrupt MDF File

Also Read: How to Fix SQL Server Header Error 5172?

Final Words

Here, we learned the whole process of how to extract data from MDF file in SQL Server Database using T-SQL and SQL Management Studio. Sometimes, the MDF file is corrupted, and retrieving data is very tough. We discussed the automated solution that allows you to extract data from corrupt MDF file with all objects.