Tuesday, May 27, 2008

FAQ : How to move a physical file (MDF or LDF) from one location to another in SQL Server 2005

In SQL Server 2000, if you want to move a physical file of a database from one location to another , you have to detach and attach the database. In SQL Server 2005, this process has been made very simple, you need to take the database offline, alter the file path with the new one using Alter Database command and copy the database file to new location manually and finally take the database online. Simple

--Step 1 : Create Database
Create Database TestMoveFile and check the database file location
GO
Select *From Sys.master_files where database_id=db_id('TestmoveFile')
GO
--Step 2 : Alter Database and Set the db to offline
Alter Database TestMoveFile Set Offline
GO
-- Step 3 : Move the physical file to new location
--Move the file to new location using dos command or Windows GUI

--Step 4 : Alter the database file path using Alter Database command
Alter Database TestMoveFile Modify File(Name='TestMoveFile',FileName='c:\TestmoveFile.mdf')
Go

-- Step 5 : Set the database Online and check the file path
Alter database TestMoveFile Set Online
GO
Select *From Sys.master_files where database_id=db_id('TestmoveFile')

Go
Drop database TestMoveFile

2 Comments:

Anonymous Anonymous said...

Thanks. Your instruction worked Perfectly for me.

8:38 AM  
Blogger Alex said...

Usually for solve problems with sql server and files too I use other tools. But yesterday no one of theirs couldn't help me. All my sql files were damaged. But luckily for me I found in the Internet - repair database sql. It helped me for seconds and for free as I kept in mind.

12:37 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home