Most of the DBAs at some point of time in their career should have faced at-least one of the below situation(s):
1. The disk/SAN where the database files are located is going to be replaced
2. Disk is full and no more free space available
3. Data files and/or log files are not located on standard drives
There can be even more secnarios like the above where we may need to move the database files from current location to a new location. Starting SQL Server 2005 this can be simply achieved by using ALTER DATABASE T-SQL command
Let us take the below scenario for step-by-step Demo:
Database: AdventureWorks2012
Current Files Location: C:\Disk1
New\Target Files Location: C:\Disk2
Step 1: Get the current database files Logical Name and Physical Location
USE master GO SELECT name AS LogicalFileName, physical_name AS FileLocation , state_desc AS Status FROM sys.master_files WHERE database_id = DB_ID('AdventureWorks2012');![]()
Step 2: Take the Database offline
USE master GO ALTER DATABASE AdventureWorks2012 SET OFFLINE WITH ROLLBACK IMMEDIATE GO
Note: In the above T-SQL query, I used WITH ROLLBACK IMMEDIATE option. Please be careful when using this option as it rolls back those incomplete transactions immediately. You may exclude this option, but have to wait till all the transactions are committed to take the database offline.
Step 3: Move the database files physically in the Windows OS to the new location
Step 4: Use ALTER DATABASE to modify the FILENAME to new location for every file moved
Only one file can be moved at a time using ALTER DATABASE.
USE master GO ALTER DATABASE AdventureWorks2012 MODIFY FILE ( NAME = AdventureWorks2012_Data, FILENAME = 'C:\Disk2\AdventureWorks2012_Data.mdf'); -- New file path USE master GO ALTER DATABASE AdventureWorks2012 MODIFY FILE ( NAME = AdventureWorks2012_Log, FILENAME = 'C:\Disk2\AdventureWorks2012_log.ldf'); -- New file path![]()
Step 5: Set the database ONLINE
USE master GO ALTER DATABASE AdventureWorks2012 SET ONLINE;
Step 6: Now, verify the database files Physical location
USE master GO SELECT name AS FileName, physical_name AS CurrentFileLocation, state_desc AS Status FROM sys.master_files WHERE database_id = DB_ID('AdventureWorks2012');![]()
Note: Same method can be used for moving files for any system or user defined database except for Resource database files
Reblogged this on saliksaly.
[…] you to Suresh Raavi, who originally posted on this topic on […]
thanks a lot its worked
Thanks, nice post
Does anything else need to be done if one of the files is moved to a different drive? Can’t get it to recognize a log file after moving from F: to C:
thanks a lot..
I would like to know when the database comes to online what are the recovery phases will work and in which order they will come to online.
Thanks. Elaborated very effectively and step by step.
Reblogged this on SIMPLE SQL DBA.
very very help full post
How to add database file when I haven’t file?
Could you please send to me this file!
My email: dinhvuong2008@gmail.com
Thanks and best wish for you!!!
Thank you
after doing all these steps, Database in recover pending mode. how to solve this issue?
[…] SQL Server – How to Move Database Files – Data File and … – SQL Server – How to Move Database Files – Data File and Log File – From One Drive Location to the Other. August 10, 2013 by Suresh Raavi […]
One step that is missing when moving to new location is the new folder requires correct permissions for SQL Server to bring the database online.
https://msdn.microsoft.com/en-au/library/jj219062.aspx
Great post!
You should change the default database path for new databases. The default path is obtained from the Windows registry.
You can also change with T-SQL, for example, to set default destination to: D:\MSSQLData
USE [master]
GO
EXEC xp_instance_regwrite N’HKEY_LOCAL_MACHINE’, N’Software\Microsoft\MSSQLServer\MSSQLServer’, N’DefaultData’, REG_SZ, N’D:\MSSQLData’
GO
EXEC xp_instance_regwrite N’HKEY_LOCAL_MACHINE’, N’Software\Microsoft\MSSQLServer\MSSQLServer’, N’DefaultLog’, REG_SZ, N’D:\MSSQLData’
GO
Extracted from: http://www.sysadmit.com/2016/08/mover-base-de-datos-sql-server-a-otro-disco.html
This does not work for me.
I cant change the file location once the database is offline.
How are users saying this has worked for them?
[…] SQL Server – How to Move Database Files – Data File and … – SQL Server – How to Move Database Files – Data File and Log File – From One Drive Location to the Other. August 10, 2013 by Suresh Raavi […]