Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Procedure expects parameter error from SqlCommandT-SQL Tuesday #13: Is that what you really want? »
    comments

    For one of the databases that I have to manage we were running out of space, so we got a shiny new 10.9 TB sized drive.

    I was asked to move some files used by one database to this new drive. I decided to write up a little blog post just in case you ever need to do this so that you don't backup and restore (with move) because there is another way.

    First create this test database with 3 data files and 1 log file, the data files will be in the C:\DB_Files directory

    1. USE master
    2. GO
    3.  
    4. CREATE DATABASE [TestMove] ON  PRIMARY
    5. ( NAME = N'TestMove', FILENAME = N'C:\DB_Files\TestMove.mdf' , SIZE = 2048KB , FILEGROWTH = 1024KB ),
    6. ( NAME = N'TestMove2', FILENAME = N'C:\DB_Files\TestMove2.ndf' , SIZE = 2048KB , FILEGROWTH = 1024KB ),
    7. ( NAME = N'TestMove3', FILENAME = N'C:\DB_Files\TestMove3.ndf' , SIZE = 2048KB , FILEGROWTH = 1024KB )
    8.  LOG ON
    9. ( NAME = N'TestMove_log',
    10. FILENAME = N'C:\MSSQL\DATA\TestMove_log.ldf' ,
    11.  SIZE = 1024KB , FILEGROWTH = 10%)
    12. GO

    Now just create a test table, insert one row and do a simple select.

    1. USE TestMove
    2. GO
    3.  
    4. CREATE TABLE Test(id INT)
    5. GO
    6. INSERT Test VALUES(1)
    7. GO
    8.  
    9. SELECT * FROM Test

    Now instead of having the data files in the C:\DB_Files we want to move them to the D:\DB_Files directory. You can use ALTER DATABASE...MODIFY FILE to do that, you just specify the new file locations, just make sure that the directory exists.

    The following code will change the location of the data files

    1. USE master;
    2. GO
    3. ALTER DATABASE TestMove
    4. MODIFY FILE
    5. (
    6.     NAME = TestMove,
    7.     FILENAME = N'D:\DB_Files\TestMove.mdf'
    8. );
    9. GO
    10.  
    11. USE master;
    12. GO
    13. ALTER DATABASE TestMove
    14. MODIFY FILE
    15. (
    16.     NAME = TestMove2,
    17.     FILENAME = N'D:\DB_Files\TestMove2.ndf'
    18. );
    19. GO
    20.  
    21. USE master;
    22. GO
    23. ALTER DATABASE TestMove
    24. MODIFY FILE
    25. (
    26.     NAME = TestMove3,
    27.     FILENAME = N'D:\DB_Files\TestMove3.ndf'
    28. );
    29. GO

    You will see the following message

    The file "TestMove" has been modified in the system catalog. The new path will be used the next time the database is started.
    The file "TestMove2" has been modified in the system catalog. The new path will be used the next time the database is started.
    The file "TestMove3" has been modified in the system catalog. The new path will be used the next time the database is started.

    [EDIT]
    Paul Randal mentioned that you don't have to shut down SQL Server, you can also just take the database OFFLINE, see here for more detail: http://www.sqlmag.com/blogs/SQLServerQuestionsAnswered/SQLServerQuestionsAnswered/tabid/1977/entryid/72328/A-Safe-Method-for-Moving-a-Database-to-a-New-Location.aspx
    [/EDIT]

    Now, the first thing you want to do is stop SQL Server or take the database offline. You can stop SQL Server in a variety of ways, if you want to use the command line (NET STOP), take a look here: Using the Command Line to manage SQL Server services. You can also use the SQL Server Configuration manager, services under Control Panel/Administrative Tools or you can use SSMS.

    To take the database offline, you can run this

    1. ALTER DATABASE TestMove
    2. SET OFFLINE;

    If you do not stop SQL Server or take the database offline, you won't be able to move the files and you will get a message like the one below


    After SQL Server is stopped, move the files to the new location

    In my case, here is where the files are currently C:\DB_Files\


    And after the move, this is the location of the files now D:\DB_Files\



    Start SQL Server again or make the database online again.

    To set the database online, run this

    1. ALTER DATABASE TestMove
    2. SET ONLINE;

    After SQL Server is up and running. run the following query.

    1. SELECT name, physical_name AS CurrentLocation, state_desc
    2. FROM sys.master_files
    3. WHERE database_id = DB_ID(N'TestMove');

    You should see something like the following

    name CurrentLocation state_desc
    TestMoveD:\DB_Files\TestMove.mdfONLINE
    TestMove_logC:\MSSQL\DATA\TestMove_log.ldfONLINE
    TestMove2D:\DB_Files\TestMove2.ndfONLINE
    TestMove3D:\DB_Files\TestMove3.ndfONLINE

    Finally, just run this simple query again to verify that you didn't corrupt anything

    1. USE TestMove
    2. GO
    3.  
    4. SELECT * FROM Test




    *** Remember, if you have a SQL related question, try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    1811 views
    Instapaper

    6 comments

    Comment from: Paul Randal [Visitor]
    Paul Randal Don't shut down SQL Server. You can just set the DB offline, copy the files, and set the DB online again. See post at A Safe Method for Moving a Database to a New Location for details.
    12/17/10 @ 10:23
    Comment from: SQLDenis [Member] Email
    SQLDenis Thanks Paul, I will add that link to this post
    12/17/10 @ 10:26
    Comment from: Nicholas Cain [Visitor]
    Nicholas Cain I put together a post on moving databases to new locations which uses the option of setting the database offline http://www.englishtosql.com/english-to-sql-blog/2010/6/23/moving-sql-datafiles.html
    12/17/10 @ 11:07
    Comment from: Erik [Member] Email
    Erik I'd love to see a walkthrough of moving indexes to new filegroups (and back?). Apparently this is the only way to get a table both fully reindexed and shrunk *at the same time*.
    12/17/10 @ 12:09
    Comment from: Manjot [Visitor]
    Manjot Hi Denis,
    Is this method safer/better than detach, move files and attach process?
    If yes, how?
    Regards...
    12/19/10 @ 17:09
    Comment from: SQLDenis [Member] Email
    SQLDenis Manjot, it is not safer or better, it is just yet another way to accomplish the same

    You can of course detach and attach
    12/19/10 @ 17:32

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)