- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
Using SQL Server Management Studio (SSMS)
Open SQL Server Management Studio and connect to the desired SQL Server instance.
In Object Explorer, right-click on Databases and select Attach.
In the Attach Databases dialog box, click Add.
Navigate to the location of the .mdf file for the database, select it, and click OK.
Verify the associated files (e.g., .ldf) in the Databases to attach grid. If a file is missing, update the path or remove it.
Click OK to complete the attachment.
Using Transact-SQL (T-SQL)
Open SQL Server Management Studio and select New Query.
Use the following T-SQL command to attach the database:
CREATE DATABASE [DatabaseName]ON (FILENAME = 'C:\Path\To\Database.mdf'),(FILENAME = 'C:\Path\To\Database_log.ldf')FOR ATTACH;Copied!✕CopyExecute the query to attach the database.
Notes
If the log file (.ldf) is missing, you can attach the database without it. Use the following T-SQL command:
CREATE DATABASE [DatabaseName]ON (FILENAME = 'C:\Path\To\Database.mdf')FOR ATTACH;Copied!✕Copy Attach a Database - SQL Server | Microsoft Learn
Aug 10, 2023 · This article describes how to attach a database in SQL Server with SQL Server Management Studio or Transact-SQL. You can use this feature to copy, move, or upgrade a SQL …
SQL Server Attach and Detach Database Examples
- You don’t need a log file to attach the database. In this scenario, the log file will be recreated. If you are planning to attach the database without copying the log file, make sure you execute a CHECKPOINT on the database before detaching the database.
- Reviews: 1
- Published: Jan 8, 2020
How to attach a database using SQL Server Management Studio
Jun 5, 2025 · How to attach a database to SQL Server. 1) Open Microsoft SQL Server Management Studio. 2) In the left pane, right-click on Databases and select Attach. 3) In the Databases to attach …
How to Attach & Detach SQL Server Database Step by Step - SysTools
Nov 18, 2025 · Learn how to attach and detach SQL Server databases using SQL Server Management studio step by step. Easily detach and attach database SQL Server if files are damaged.
How to Attach and Detach Database in SSMS
Learn how to attach and detach a database in SQL Server using SQL Server Management Studio (SSMS). Step-by-step tutorial for beginners: detach …
How do I attach a database in SQL Server?
When you want to remove a database that is no longer actively used but have the ability to attach later as needed. Don't have the space to backup or to restore …
- People also ask
SQL SERVER – Attach a Database with T-SQL - SQL Authority with …
Dec 18, 2020 · Let us see today we can attach a database with T-SQL. There are two different methods to attach the database. Let us see both of them. This is my preferred method and I use it all the time. …
SQL Attach Database - Tutorial Gateway
In this section, we will see the step by step approach to attach a Database in SQL Server. For this demonstration, we are going to use the existing stored procedure, …
Attach a database in SQL Server - Devart
Dec 11, 2025 · This topic describes how to attach a database using dbForge Studio for SQL Server. You can also use this feature as part of the process to copy, …
Attach and detach SQL databases with SQL server management studio
To attach a database: Select the database file you want to attach and click OK. If you see a message about a missing LDF file, remove the file and proceed. SQL will recreate the LDF file. Open your CS …