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

Author | October 30th, 2019 | 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. A .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 know how to extract data from corrupt MDF file using SQL recovery software which helps to Recover SQL Server Database MDF files with all elements.

Download SQL Recovery

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 following section introduces both methods. You can easily choose one of them.

#Method 1: Using Transact-SQL

Run the following command to retrieve data from MDF file:

CREATE DATABASE Mydatbase ON

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

FOR ATTACH_REBUILD_LOG

GO

The above command attach the database to SQL Server even if 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

Follow these simple step by step process to perform this task:

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 then 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 then 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. To do this, you can go to the location of the .mdf file and right-click the .mdf file, then 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 MDF file which is in a healthy state. But what to do if the MDF file is highly corrupted? The above method is not able to do so. In such a case, 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. With this tool, you can easily recover MDF file of SQL Server 2019, 2017, 2016, 2014, 2012, and earlier versions.

This application offers the facility to scan and preview all database items. One can easily extract data from corrupt MDF file and restore directly to the live SQL Server Database or Compatible SQL Scripts. It provides an option to restore MDF file to a new database or an existing database without loss of data.

Step by Step Process to Extract Data from Corrupt MDF File

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

Open Database File

Step 2. Select the Quick or Advance Scan Mode and then select the version of the SQL .mdf file.

Select SQL Server .mdf file version

Step 3. The scanning process will start. Once it’s done, you can see the preview of recovered data stored in the MDF file.

Preview Database Objects

Step 4. Export the data to SQL Server Database or SQL Compatible Scripts according to your needs. Enter the required information to proceed.

Restore SQL Database

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

Extract Data from Corrupt MDF File

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 due to many reasons and retrieving data is very tough. Therefore, we also discussed the automated solution that allows you to extract data from corrupt MDF file with all objects.