views:

411

answers:

3

I'm not looking to relocate the database to another server entirely, but just move the data file(s) and log file to another drive with more space. I've seen conflicting directions on how to do this, so I'm looking for the recommended proper way of doing it.

+3  A: 

Did you take a look at this?
http://support.microsoft.com/kb/224071

Rich
Detaching and reattaching in the new location is usually the best way to go
Anthony
Very nice. I found another MS article, but it was pre-2005. This link had all the information I needed and it worked. Thank you!
Kilhoffer
+1  A: 

To be absolutely safe, I would do the following:

  1. Backup the database to a BAK file.
  2. Take the current database offline, or delete it if you want to.
  3. Restore the database and change the location of the MDF and LDF files.

Scripts sample:

-- Get the file list from a backup file.  
-- This will show you current logical names and paths in the BAK file
RESTORE FILELISTONLY FROM disk = N'C:\Backups\MyDatabaseName.bak'

-- Perform the restore of the database from the backup file.  
-- Replace 'move' names (MDFLogicalName, LDFLogicalName) with those found in 
-- the previous filelistonly command
restore database MyDatabaseName
from disk = N'C:\Backups\MyDatabaseName.bak'
with move 'MDFLogicalName' to 'D:\SQLData\MyDatabaseName.mdf',
     move 'LDFLogicalName' to 'D:\SQLLogs\MyDatabaseName_log.ldf',
replace, stats=10;

Notes

The first script will get you the current names and paths that you'll need in the second script. The second script restores the database back to the name you want it to have, but you can change where you want to store. In the example above, it moves the MDF and LDF files to the D: drive.

Jay S
One problem with this approach is that it requires much more time for the backup/restore. Restores will always take longer than copying the mdf/ldf and reattaching. It can become prohibitive with DBs of any significant size
Anthony
Agreed, but whenever I start messing around with the databases, especially when there are so many developers or websites dependent on them, I like to go through backup files just in case something goes wrong and I need to get back to a good state.
Jay S
+1  A: 

Another way - detach database files (database->tasks->detach), move them to new drive and then attach again. But way described by Jay S is the simpliest.

Alex_L