Home » How To » How to Attach Database File in SQL Server? Best Solutions

How to Attach Database File in SQL Server? Best Solutions

Published By Kumar Raj
Aswin Vijayan
Approved By Aswin Vijayan
Published On November 15th, 2021
Reading Time 4 Minutes Reading

Summary: Are you finding a solution for how to attach database file in SQL Server? If yes, then have patience & go through this article to learn the step-by-step procedure to connect database to SQL Server 2019, 2017, 2016, 2014, 2012, and other versions in the simplest way.

In this technological era, SQL Server is a well-known database management system. It gives variability and flexibility to the database administrator to store and manage their data. Sometimes, users confront the need to attach database file in SQL Server. Users can easily connect database file if it is in a good or healthy state. But, if corruption occurs in the required database files then it can be a nightmare for users.

Therefore, the major aim of this post is to show you the best and easiest way that can help you attach database in SQL Server without hassles. After executing the given steps you will surely amazed at how easy this can be done. so let’s proceed with this.

How to Attach Database In SQL Server?

In the following section, you will get the different methods to attach database file in SQL Server. But the manual methods will only work in the case of the healthy database file. If the database MDF file you want to connect is corrupt, damaged, or inaccessible then it is recommended to use the professional solution to get done this job in an easy manner.

Manual Ways to Attach Database File In SQL Server

Method #1: Using SSMS

  1. Launch SQL Server management studio. Go to the Object Explorer section and right-click on the Database folder after that choose the Attach from the menu.
  2. In the Attach Databases Wizard, click on the Add button to proceed.
  3. Browse the location of the database MDF file, select the desired file to attach, and then hit the OK.
  4. Afterward, an Attach Database screen will appear where you see the database details.
  5. At last, tap the OK to attach database MDF file in SQL server.

Method #2: Using T-SQL Query

To perform this approach you need to run a T-SQL Script on the SQL Query page. Use the script mentioned below:

USE Master
ON (FILENAME = ‘C:\Program Files\Microsoft SQL Server\..\MSSQL\DATA\’Database_name.mdf’),
(FILENAME = ‘C:\Program Files\Microsoft SQL Server\..\MSSQL\DATA\’Database_name.ldf’)

Expert Solution to Attach Corrupted Database in SQL Server

The manual approaches discussed in the above section do not work for the corrupted database file. In that case, you can opt for enterprise grade-level professional SQL Recovery Software. The software can repair corrupt SQL database MDF and NDF files and allow users to export recovered data directly to the live SQL Server within the network.

With this tool, users can efficiently fix the database corruption issues from MDF & NDF files and export the recovered data/files to SQL Server. Also, users can run this application on any Windows OS (32-bit and 64-bit) and it supports all versions of SQL Server 2019, 2017, 2016, 2014, and all its below versions.

download SQL Recoverypurchase

Steps to attach database file in SQL server 2019, 2017, 2016, 2014, 2012

1. Download, install and launch the tool in your system, after that click the Open to add your MDF file.

Attach database file in SQL Server

2. Choose a file scanning option (as per the level of corruption) and also select the version of database MDF file.

attach database file in SQL server
3. Once the file scanning gets completed, preview the complete database and click the Export button.

attach database file
4. Select the export to SQL Server database option and fill the desired credentials.

attach database file in SQL server
5. Now, choose the desired items from the export Window and finally, click the Export button.

attach database file in SQL server

Some Advance Features of the Software:-

  • It can repair corrupted database items like views, stored procedures, triggers, functions, and tables.
  • Promises MDF file recovery in two different modes: Quick and Advanced.
  • Recovers both Microsoft SQL Server database NDF and MDF files easily.
  • Provide option to Export SQL Server Data to SQL Script.
  • Allows the user to preview all the data items of database files before saving.
  • The software does not impose any file size limitation to repair MDF files on Windows.
  • Widely compatible with all versions of Windows Operating System and SQL Server 2019, 2017, 2016, 2014, 2012, and below.

Read More: How to Recover Deleted Objects in SQL Server?

Wrapping up

In this write-up, we have mentioned the best and quick solutions to attach database file in SQL Server. The described manual methods will work only in case of the database is cleanly shut down and the required database MDF file is corruption-free. In case of corrupt or damaged database files, it is suggested to take the help of a professional solution to achieve perfect results.