Read the Process to Attach a Database in MDF File of SQL Server

SQL Server MDF file contains the backup of all the following database elements:

  • Tables
  • Stored procedures
  • Views
  • User permissions for the database etc

An MDF file is basically attached to a database. Fundamentally, this file works to restore the database back to the Server. This also proves beneficial in case you wish to move a particular database from one specific Server machine to some other machine. MDF file gets attached to the Server, and then, it becomes usable on the network for retrieving database records for your applications.

Process to Attach a Database in MDF File

  • Go to Start >> All Programs
  • Click “SQL Server” in order to open the list of SQL Server tools
  • Click “SQL Server Management Studio” shortcut for opening the main management console of the Server
  • Click the database Server name
  • In the toolbar at the top, click the “New Query” button
  • This will open up a new console Window, wherein, you can execute the SQL code, including the code for attaching the MDF
  • Enter the following code in the SQL query Window:

EXEC sp_attach_single_file_db @dbname='database_name',@physname='C:\mdfFile.mdf' GO This code executes an internal SQL Server stored procedure named "sp_attach_single_file_db"

(@dbname variable is used for naming the database after it gets attached and the "@physname" value is basically the hard drive location for your SQL MDF file)

SQL Recovery Software

If you are attaching your database in MDF File and you successfully attached your database in MDF file but, if your database gets corrupt due to some unavoidable reasons then you need to repair that corrupt database, and it’s possible when you have successful SQL MDF database recovery software. To repair or recover your corrupt database of SQL Server you can try SQL Recovery Software with no doubts.


Download Purchase