Let us Consider we have two folders location1 and location2. We want to move database files from loc1ation1 to location2.
- Keep database in single user mode
USE MASTER;GOALTER DATABASE ProductionSET SINGLE_USERWITH ROLLBACK IMMEDIATE;GO- Detach DB
EXEC MASTER.dbo.sp_detach_db @dbname = N'Production'GO - Now move the files from location1 to location2. You can now reattach the files with new locations.
-- Move MDF File from Location1 to Location2-- Re-Attached DBCREATE DATABASE [Production] ON( FILENAME = N'F:location2Production.mdf' ),( FILENAME = N'F:location2Production_log.ldf' )FOR ATTACHGO