Click here to Skip to main content
15,886,963 members
Articles / Database Development / SQL Server
Tip/Trick

Move Database File from One Drive to Another Drive

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
17 Oct 2018CPOL 5.2K   3   3
Moving database file from one drive to another drive

Introduction

Sometimes, we need to change the database file location from one drive to another drive. Here are some simple tips on how we can achieve this goal.

Steps

  1. Suppose you have a database file named Test and you want to move it to other drive from the default location. First, run the following query to get information about the database file.
    SQL
    select * from Test.sys.database_files

    Using databse_files view, you can get information about databasefile such as Name, physical_name, state_desc. From physical_name column, you get the current location of the database file.

  2. Run database OFFLINE script:
    SQL
    use master
    alter database Test set OFFLINE
  3. Move database file (both .mdf, .ldf) to the desired location. In my case, I have moved my files in D drive SqlDatabase folder (D:\SqlDatabase).
  4. Update database and modify File. Set FileName as your desired location:
    SQL
    ALTER DATABASE test 
    MODIFY FILE (
        NAME = 'test',
        FILENAME = 'D:\SqlDatabase\Test.mdf'
    )
    
    ALTER DATABASE test
    MODIFY FILE (
        NAME = 'test_log',
        FILENAME = 'D:\SqlDatabase\Test_log.ldf'
    )
  5. Change the permission of these files and give full control to Users Group.

    Image 1

  6. Run database online script:
    SQL
    ALTER DATABASE Test SET ONLINE 

By following these simple steps, you can move your database from one drive to another.

Points of Interest

History

  • 18th October, 2018: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionOther way Pin
Alen Toma19-Oct-18 3:46
Alen Toma19-Oct-18 3:46 
GeneralMy vote of 5 Pin
Alek Massey18-Oct-18 9:36
Alek Massey18-Oct-18 9:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.