Moving Database files by Offline method in SQL 2005

AID: 1643
  • Status: Published

1740 points

  • Byprabhakaranbk
  • TypeTips/Tricks
  • Posted on2009-09-25 at 22:43:03
Moving Database files by Offline method
-Prabhakaran
This document will show you how to move the database files without detaching the databases as we do in SQL 2000.
This method will be applicable for only SQL 2005\SQL 2008 databases

To move files, we have to specify the current logical name of the file and the new file path, which includes the new file name.
I would like to tell important point in this method we can move only once file at a time in this manner.
To move data or log files to new location, follow these steps

1.      Take a sp_helpfile output of the database, which will provide the logical file name of the data and log files.
sp-help.jpg
  • 1.6 MB
  • sp_help
sp_help

 
2.      Set  the database you want to work with offline by doing below
 ALTER DATABASE dbadb SET OFFLINE 
                                    
1:

Select allOpen in new window


offline.jpg
  • 1.6 MB
  • offline
offline

 
3.      Move one file at a time to the new location
In Windows Explorer physically move the file to the new location, and then tell the SQL Server Database where that file now lives.
 
  ALTER DATABASE dbadb
 MODIFY FILE(NAME=dbadb,FILENAME='E:\Data\MSSQL\dbadb.mdf')
 GO 
                                    
1:
2:
3:

Select allOpen in new window


modifyfile.jpg
  • 1.7 MB
  • modify file
modify file

You can move only one file at a time, So if you want to move the more than one file like data and log files, first move the file using the step3 described above then repeat the same step for other files of the database.
 
4.      Set back the database to online
 
 ALTER DATABASE dbadb SET ONLINE   
                                    
1:

Select allOpen in new window


setonline.JPG
  • 58 KB
  • online
online

Before bringing the database to offline there should be no user connections to database either you can do this by killing all the connections or by issueing the following statement
 
 ALTER DATABASE dbadb SET SINGLE_USER WITH ROLLBACK IMMEDIATE 
                                    
1:

Select allOpen in new window



 
  ALTER DATABASE dbadb SET MULTI_USER WITH ROLLBACK IMMEDIATE 
                                    
1:

Select allOpen in new window


   
5.      Verify the move by checking sp_helpfile output of the database again and you can observe the new path.
output.JPG
  • 67 KB
  • verify
verify

 
See the highlighted area in Red, It shows the current file path.
  • There are significant advantages of this method which I feel is,
    There is no possibility of dbid mismatch in this method; change in dbid will cause login failure for the logins which have default db as the db you have detached.

  • The database owner will not change but in detach and attach method you need to record db owner before detaching database to exactly match the db owner while attaching database.

  • Cross database ownership chaining property will be retained in this method where as in detach and attach method this property will be lost while you attach the database.

  • This method seems faster than detach and attach method


Hope this simple tip will help you guys.

Disclaimer - This is the way I used to move the files in our environment and it works perfect, But you should test this method before applying on the Production systems
    Asked On
    2009-09-25 at 22:43:03ID1643
    Tags

    Moving Database files by Offline method

    Topic

    SQL Server 2005

    Views
    1205

    Comments

    Add your Comment

    Please Sign up or Log in to comment on this article.

    Join Experts Exchange Today

    Gain Access to all our Tech Resources

    Get personalized answers

    Ask unlimited questions

    Access Proven Solutions

    Search 3.2 million solutions

    Read In-Depth How-To Guides

    1000+ articles, demos, & tips

    Watch Step by Step Tutorials

    Learn direct from top tech pros

    And Much More!

    Your complete tech resource

    See Plans and Pricing

    30-day free trial. Register in 60 seconds.

    Loading Advertisement...

    Top MS SQL Server 2005 Experts

    1. ScottPletcher

      195,617

      Guru

      8,500 points yesterday

      Profile
      Rank: Genius
    2. jogos

      176,191

      Guru

      668 points yesterday

      Profile
      Rank: Sage
    3. acperkins

      140,953

      Master

      1,000 points yesterday

      Profile
      Rank: Genius
    4. TempDBA

      113,707

      Master

      1,168 points yesterday

      Profile
      Rank: Sage
    5. matthewspatrick

      93,824

      Master

      1,600 points yesterday

      Profile
      Rank: Savant
    6. lcohan

      93,302

      Master

      2,000 points yesterday

      Profile
      Rank: Genius
    7. dtodd

      84,612

      Master

      0 points yesterday

      Profile
      Rank: Genius
    8. mwvisa1

      76,166

      Master

      0 points yesterday

      Profile
      Rank: Genius
    9. ValentinoV

      76,011

      Master

      1,800 points yesterday

      Profile
      Rank: Genius
    10. ralmada

      55,844

      Master

      400 points yesterday

      Profile
      Rank: Genius
    11. anujnb

      54,164

      Master

      0 points yesterday

      Profile
      Rank: Wizard
    12. angelIII

      53,846

      Master

      10 points yesterday

      Profile
      Rank: Elite
    13. EugeneZ

      53,602

      Master

      0 points yesterday

      Profile
      Rank: Genius
    14. HainKurt

      49,150

      0 points yesterday

      Profile
      Rank: Genius
    15. Buttercup1

      48,568

      0 points yesterday

      Profile
      Rank: Master
    16. huslayer

      40,600

      0 points yesterday

      Profile
      Rank: Sage
    17. appari

      39,400

      0 points yesterday

      Profile
      Rank: Genius
    18. tim_cs

      34,200

      0 points yesterday

      Profile
      Rank: Wizard
    19. wdosanjos

      33,836

      0 points yesterday

      Profile
      Rank: Genius
    20. dqmq

      31,136

      0 points yesterday

      Profile
      Rank: Genius
    21. Cluskitt

      30,940

      0 points yesterday

      Profile
      Rank: Wizard
    22. SJCFL-Admin

      30,877

      0 points yesterday

      Profile
      Rank: Master
    23. jimhorn

      29,975

      0 points yesterday

      Profile
      Rank: Genius
    24. Brichsoft

      28,107

      0 points yesterday

      Profile
      Rank: Sage
    25. momi_sabag

      27,903

      0 points yesterday

      Profile
      Rank: Genius

    Hall Of Fame