Published on

January 30, 2020

How to Attach SQL Server MDF Files

In SQL Server, attaching MDF files is a common task when working with databases. MDF files are the primary data files that store the actual data and structure of a database. In this article, we will explore different methods to attach MDF files and troubleshoot any errors that may occur.

Understanding Database Files

Before we dive into the attachment process, let’s first understand the different types of database files in SQL Server:

  • Primary Data File (MDF File): This is the main data file that stores the columns, fields, rows, indexes, tables, and application data of a database.
  • Secondary Data File (NDF File): These are optional files used to stripe data across multiple drives of the database server.
  • Log File (LDF File): This file stores the changes made in the database, such as insert, update, and delete operations. It is essential for database recovery.

Finding the Location of MDF Files

To attach an MDF file, we first need to know its location. We can obtain this information by querying the system views sys.database_files and sys.master_files. The former provides information specific to a database, while the latter provides the physical location of all databases.

Here is an example query to retrieve the location of MDF files from sys.master_files:

USE master;
SELECT db_name(database_id) AS [Database Name],
       type_desc AS [File Type],
       physical_name AS [Database File Location]
FROM sys.master_files
WHERE database_id >= 5; -- Exclude system databases

We can also use the sys.database_files view to obtain the information of the database files for a specific database:

USE WideWorldImporters;
SELECT db_name() AS [Database Name],
       type_desc AS [File Type],
       physical_name AS [Database File Location]
FROM sys.database_files;

Alternatively, we can view the database files location using SQL Server Management Studio (SSMS) by following these steps:

  1. Open SSMS and connect to the database engine.
  2. Expand the “Databases” folder.
  3. Right-click on the desired database (e.g., “WideWorldImporters”) and select “Properties”.
  4. In the properties window, select the “Files” tab.
  5. The “Path” and “File Name” columns will display the location of the database files.

Attaching MDF Files

There are two main methods to attach MDF files: using SQL Server Management Studio (SSMS) and using T-SQL scripts.

Method 1: Attach MDF File using SSMS

To attach a database using SSMS, follow these steps:

  1. Open SSMS and connect to the database engine.
  2. Right-click on the “Databases” folder and select “Attach”.
  3. In the “Attach Database” dialog box, click on “Add” to locate the MDF files.
  4. In the “Locate Database Files” dialog box, navigate to the desired MDF file(s) or provide a custom location.
  5. Select the desired MDF file(s) and click “OK”.
  6. Review the database details in the “Database to attach” and “Database file details” boxes.
  7. Click “OK” to attach the database.

Once the database is successfully attached, you can view it in the object explorer of SSMS.

Method 2: Attach MDF File using T-SQL Query

We can also attach MDF files using T-SQL commands. Here are two examples:

Using “CREATE DATABASE.. WITH ATTACH” command:

USE master;
CREATE DATABASE [DatabaseName]
ON (NAME = 'LogicalName of the Data file', FILENAME = 'Data File Name'),
   (NAME = 'LogicalName of the Log file', FILENAME = 'Log File Name')
FOR ATTACH;

Using “exec sp_attach_db” command:

USE master;
EXEC sp_attach_db @dbname = 'DatabaseName',
                  @filename1 = '<Location of the database file>',
                  @filename2 = '<Location of the Log file>';

Troubleshooting Errors

While attaching MDF files, you may encounter some common errors. Here are two examples and their possible solutions:

Error: Access denied due to lack of permission

This error occurs when the user does not have sufficient permissions on the database file or log files. To fix this, you can try the following solutions:

  • Run SQL Server Management Studio as an administrator and attach the database.
  • Explicitly grant full control access to the MDF file and LDF file of the database by right-clicking on the files, selecting the “Security” tab, and granting full control to the appropriate user.
  • If the above solutions do not work, copy the database files to the default database file locations. This will automatically grant the user the required permissions.

Error: Unable to downgrade

If you see the error message “The database cannot be opened because it is version XXX,” it means you are trying to attach database files of a higher version to a lower version of SQL Server. Unfortunately, downgrading is not supported by Microsoft, so there are no options to rectify this issue.

Summary

In this article, we have explored different methods to attach SQL Server MDF files. We have learned about the different types of database files, how to find their locations, and how to attach them using SSMS and T-SQL scripts. We have also discussed common errors that may occur during the attachment process and provided troubleshooting steps. By following these guidelines, you can easily attach MDF files and work with your SQL Server databases.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.