Published on

January 21, 2020

How to Move a SQL Database Using the Detach and Attach Method

Introduction:

There are various scenarios where you might need to move a SQL database, such as lack of free space, database management, database movement, or database upgrade. In this article, we will discuss the detach and attach method, which is one of the ways to move a SQL database.

Move a database using detach and attach method:

1. Collecting SQL Database information:

Before moving a database, it is important to gather information about the existing files and their locations. You can use the sp_helpfile command or the sys.database_files catalog view to fetch the required information.

2. Detach a SQL database:

In this method, you detach the database from the source instance. Right-click on the desired database in SSMS and click on Tasks->Detach. Make sure there are no active connections to the database. You can choose to drop connections and update statistics before detaching the database. It is recommended to generate a script using the detach database wizard and execute the script.

3. Move SQL Database files into the desired location:

Once the database is detached, you can move the database files from the old location to the new location. It is recommended to use copy-paste instead of cut-paste for flexibility and rollback options.

4. Attach SQL Database files from the new location:

After moving the database files to the desired location, connect to the new SQL instance, right-click on the Databases folder, and choose the Attach command. Navigate to the location of the database files and select the MDF file. Validate the path of all database files and modify if required. It is also recommended to generate a script for future reference.

Limitation of Attach and detach method:

– System databases (master, model, msdb, and tempdb) cannot be detached.

– This method cannot be used for replicated, mirrored, or snapshot databases.

– A suspect database cannot be moved.

– You cannot attach a database to a lower version of SQL Server.

Conclusion:

In this article, we explored the detach and attach method to move a SQL database. It is important to review all available options and choose the appropriate method based on your specific requirements. The detach and attach method provides a straightforward way to move a database while preserving its data and log files.

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.