Move User Database Files using Alter Database Command

For moving User database files using alter command , You need to follow these simple steps in same sequence.
1. Put DB in single User Mode using below query
Alter database Dbname
set single_user

If there is some connection openned with DB then use below query to check connections
select DB_Name(database_id) as DBName,* from sys.dm_exec_requests
where session_id>=51 and database_id=DB_Id('DBName')
If you find any open connection with DB then you can kill the process
2. Take User DB offline
alter database Dbname
set offline
3. Now copy mdf and ldf to new location and move DB files location in master DB using Alter Database Command
4.To view logical name of DB files, run below query
select * from sys.master_files
Run below query to in update location of DB files master DB
Alter Database DBname
modify file(Name='Testing', filename='D:\DB_Backup\DBName_DB\Testing.mdf')
Alter Database DBname 
Modify
file(Name='Testing_log',filename='D:\DB_Backup\DBName_DB\Testing_log.mdf')
5. Take User DB online
Alter database Dbname
set online
6. Now check DB files location
Use DBName
Select * from sysfiles
7. Now Set DB in multi-User mode
Alter database Dbname
set multi_user

0 comments:

Comment On Facebook