Get IT Solutions

How to do IT
Menu
  • Home
  • SCCM 2012
    • Deploy Packages
    • Troubleshooting errors SCCM 2012
  • Windows
    • Applications Silent Install
    • Windows Tools
    • Windows Error
    • Script
    • Exchange Server
    • Troubleshooting Office
    • Applications Errors
  • Database
    • SQL Server
    • MySQL
    • Oracle
  • Cybersecurity
  • Other
  • Reviews

Sql server move database files to another drive in fastest way – data and log

Sometimes we need on SQL Server move database files to another drive. In this article we will try to explain 3 easy methods to perform this task. The first method explains moving of database files using SQL Server Management Studio Interface. Going step by step without any script. The other two methods are based on script. The first uses the method of “ALTER DATABASE”. The second uses “Detach/Atach” method.

Why will need to move sql databases on another drive?

First scenario: All your databases ate located on D: partition and partition is running out of the space. You are going to create new Partition E: and need to allocate some of data and log files to the new partition.

Second Scenario: Database creator has used some T-SQL Script to create databases. T-SQL script created database on default location that was specified on SQL Server Instance and most likely is on C:. Microsoft recommended to use different partition from where OS is installed for better performance. Also different partition for log file (LDF) and data file (MDF).

Read also other SQL Server Helping Posts:

  1. How to fix SQL Server Error 18456
  2. Fix SQL server error 26 and error 40
  3. How to disable all sql jobs at once.
  4. How to FIX SQL Error 17002

Sql server move database files to another drive

  • Using SQL Server Management Studio Interface to move database files.
  • Script to move database files with “ALTER DATABASE” method.
  • Moving database files with “Detach/Atach” method using T-SQL Script

 

Using SQL Server Management Studio Interface to move database files.

Before you can start with sql server move database files to other location need to be checked:

  • Please perform a Full Backup before you move any database.
  • Login User need sysadmin server-role to do the following changes.
  • The default database of login user different from the database that will be moved.

Locate the path of the data and log files that will be moved.

  1. You can use database properties to identify the path:

Database name – Properties – Files (Under the “path” is Path of files)

Locate the path of the data and log files

Locate the path of the data and log files

  1. Or use the following T-SQL script:

SELECT name, physical_name AS current_file_location

FROM sys.master_files

where Name in ('test','test_log')

Change the name of the database or remove “where Name in (‘test’,’test_log’)” to get all database files path.

 

Locate the path of the data and log files

Locate the path of the data and log files

The default located of data and log files is:

C:\Program Files\Microsoft SQL Server\MSSQL11.SGALDW\MSSQL\DATA\Test.mdf

Starting the move of the database:

Go to SQL Server Management Studio and find the database that you will move data and log files.

Right Click – Task – Detach

detach database sql server

Detach database sql server

The following view will appear:

Drop Connection

Drop Connection

Need to check Drop Connection box to be sure that we didn’t have any connection during detach the database. In default Detach command will not perform if any connection exist. After click OK the database will be detached and will disappear from databases view on Management Studio.

READ ALSO -   Sql job failed to notify via email – Error SQL Server Agent

Go to the directory where database files stored. Cut and paste to the new destination folder on different drive. Like the example below:

Copy Database Files

Copy Database Files

After the copy finished go back to Management Studio and Right click to the database and after that click Attach…

Sql Server Attach Database

Sql Server Attach Database

Find the data file on the new destination and after add the data will be filled automatically.

Move sql database to another drive

Move sql database to another drive

Click Ok and the new database will appear on Management Studio. Do some select from the tables to be sure that everything is ok.

 

Sql Server move database files with “Alter Database” method.

You can use T-SQL commands to move data and log file of the sql database in another drive.

Please follow steps to move database to a new location.

  1. Find the path of the data and log file explain previously:

Use master

SELECT name, physical_name AS current_file_location

FROM sys.master_files

where Name in ('test','test_log')
  1. Change the database you want to move to offline with the command:

ALTER DATABASE test SET OFFLINE WITH ROLLBACK IMMEDIATE;

Note! To kick out everyone and rollback all currently open transactions need to use “WITH ROLLBACK IMMEDIATE”

  1. Move files to the new location with the following command:

-- ms sql move database to another drive
ALTER DATABASE test MODIFY FILE ( NAME = test, FILENAME = " C:\Program Files\Microsoft SQL Server\MSSQL11.SGALDW\MSSQL\DATA\Test.mdf") GO

ALTER DATABASE test MODIFY FILE ( NAME = test_log, FILENAME = " C:\Program Files\Microsoft SQL Server\MSSQL11.SGALDW\MSSQL\DATA\Test_log.ldf") GO
  1. Change the database status to Online:

ALTER DATABASE test SET online GO

In fact this method its fastest that using Management Studio interface.

 

SQL Server move database files with Detach/Attach method using T-SQL

The last method use Detach/Attach to move SQL server database file. We are going to explain it step by step.

Our goal Is to move database files from Source “C:\Program Files\Microsoft SQL Server\MSSQL11.SGALDW\MSSQL\DATA\Test.mdf” to destination “D:\New_Destination\”

  1. Detach databse command

-- move sql database to another drive
USE MASTER;
GO
-- Take target database in single user mode.
-- This may disconnect your active transactions for target database
ALTER DATABASE test
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
-- Detach targetDB
EXEC MASTER.dbo.sp_detach_db @dbname = N'test'
GO
  1. In this step move the data and log files of target database from source folder to the new destination.
Move sql database files to another drive

Move sql database files to another drive

  1. Attach the target database with the new location folder

-- Sql server Move data and log files from source to the new destination folder
-- Re-Attached DB
CREATE DATABASE [test] ON
( FILENAME = N'D:\New_Destination\test.mdf' ),
( FILENAME = N'D:\New_Destination\test_log.ldf' )
FOR ATTACH
GO
  1. After this the database will attached and you can check on Management Studio Databases. Do some select from the tables to be sure that everything is ok.

Note! Execute command “ROLLBACK IMMEDIATE” only if you are sure that not going to disconnect any important connection.

Share
Tweet
Google+
Pinterest
Linkedin
Stumble
Email
Prev Article
Next Article

Related Articles

Sql Server Verion
Sometimes we need to find sql server version number, edition …

Find SQL Server Version Query – CMD – Edition and Service Pack

SQL Server error 233
When try to access the database sometimes on users appear …

How to Fix Microsoft SQL Server error 233 – Login failed

Best Computers & Laptops

Best Computer for Cyber Security – Laptop and Desktop
Best Computer for Cyber Security
Hey, are you seeking the best computer for cyber security? Well, we must say you have come to the right place to find your answer and guidance. There are a plethora of products out there for you to pick from ...
Read More
Best Computer for Microsoft Office & Excel – Laptop and Desktop
Best Computer for Microsoft Office & Excel
Are you finding the Best Computer for Microsoft Office and Excel? In modern days, the best laptop or desktop set is the one which is both portable, has good looks and offers the smoothest performance. Microsoft Office is a package ...
Read More
Best Computer for Hacking – Laptop and Desktop Selection
Best computer for Hacking
When it comes to hacking, either laptop or desktop, choosing the best computer for hacking would be an exhilarating job. There are many specifications to be considered for hacking ...
Read More

SQL Server Tips

FIX sqlstate 42000 – mysql error 1064 – you have an error in your sql syntax
Sqlstate 42000 Is a general code that come together with other number. Most often comes with the code 1064 and ...
Read More
The execute permission was denied on the object – SQL Server Error
The execute permission was denied on the object
The following article will handle “the execute permission was denied on the object” error appears on SQL Server. This error ...
Read More
How to Read Data from LDF file in SQL Server – A Complete Guide
Read LDF File
Hello Everyone, Today we are going to address the most popular query of SQL users, how to read data from ...
Read More

Search

We are on:

Get FREE SPACE for your PC

3 Method to Delete Temp Files in Windows 7/10 including vbs script

In this post we will explain how to delete temp files windows 7 using three different methods including vbs script ...
Read More

Fastest way to delete hiberfil sys from windows 10/8/7 and XP – CMD

In this article we will explain how to remove or delete hiberfil sys from Windows 10/8/7 and XP . If you does ...
Read More

Learn How to Silent Install Applications

Silent Install Adobe Flash Player
In this tutorial will explain how to silent install adobe flash player 25, Msi and Exe version. How to disable auto update and uninstall older ...
Read More
Java Silent Install and Uninstall Older Version – Deploy Package MSI
This tutorial will explain how to silent install Java MSI/EXE version and disable auto-update. The command line will also uninstall the ...
Read More
Silent Install Adobe Shockwave Player 12 and disable update
In this tutorial will explain how to silent install Adobe Shockwave Player 12 msi and disable auto update. The command will uninstall older ...
Read More
Silent Install Google Chrome MSI, Silent Uninstall and Disable Auto Update
This tutorial will explain how to silent install Google Chrome MSI and disable auto update. The command will uninstall older version of ...
Read More
Silent Install Adobe Reader 11 and DC – msi and exe – disable update
In this tutorial will explain how to silent install Adobe Reader version 11 and DC. We will user both version "msi" ...
Read More
Silent Install Mozilla Firefox msi and exe file – Including Silent Uninstallation
This tutorial will explain how to silent install Mozilla Firefox. We will use MSI and EXE files to perform a silent ...
Read More
Skype Silent Install Msi and disable updates
In this tutorial will explain how to silent install skype  and disable auto update. The command will uninstall older version of Skype and ...
Read More
Filezilla Silent Install msi and exe version
Filezilla silent install tutorial will explain how to perform a silent installation of application. We will explain methods, silent install ...
Read More
Silent install VLC Media Player
In this tutorial will explain how to silent install VLC Media Player. We will explain both methods for exe and ...
Read More
Notepad++ silent install exe and msi version – Command Line
Notepad++ silent install is the new article from a series of silent installations. Notepad++ as free software has two main ...
Read More

Batch File Solutions

How to list files in cmd – Command Prompt – Windows 10
Whenever you want to search and make a list of all files on a specific folder, you used the windows ...
Read More
Batch rename multiple files in folder – CMD script – Bulk Method – Win 10
The following article will teach how to rename multiple files in a folder with the bulk method using Command Line ...
Read More
Batch script rename file using Command Line (CMD) & PowerShell – Windows 10
The following article will use the “rename” or “ren” command to rename the file using a command prompt. Also, we ...
Read More
Change extension of multiple files at once – CMD batch file
The following article will teach the methods to change the extension of multiple files at once using command prompt and ...
Read More
How to delete registry key with command line | PowerShell | Batch
The following article is the third of series about registry and working on it through command line (CMD) and PowerShell ...
Read More
How to add registry key & values with CMD | PowerShell | Batch
The registry is the place where most of the applications store the settings but not only. Used also from the ...
Read More
Unmap Network Drive CMD – (Batch file) – net use delete command
After we posted the methods to map network drive using cmd commands we come this time with the method to ...
Read More
Map Network Drive cmd (batch file) – net use user password
This article will focus on another way to map a network drive on your computer instead of from the explorer ...
Read More
How to Find Large Files on Windows 7 & 10 – CMD Forfiles Command
If the capacity of your hard drive is running low, it is time to clean off some files and to ...
Read More
CMD Script to check disk space on windows and multiple remote servers
The following article will provide you script to check disk space. The Script monitor space on windows and multiple remote ...
Read More

Get IT Solutions

How to do IT

About Us

Get IT Solutions is a personal blog, which is managed to guide people for various topic.

Second Menu

  • Donate
  • About Us
  • Contact Us
  • Privacy Policy

What Will You Find

Automation is our area of writing where are included scripts, batch and various tips to automate your daily job.
Copyright © 2025 Get IT Solutions