Solved

sql I moved database files, how do I remap the dbs

Posted on 2012-12-29
6
710 Views
Last Modified: 2012-12-29
I had originally created my databases by copying the files and running the sql below.  I had to move the files to a different drive.

They were in: C:\OptionDB
They are now in : J:\OptionDB

Here is the command that I used to create the database.

CREATE DATABASE HOD201210
  ON(NAME='HOD201210', FILENAME='C:\OptionDB\HOD201210.mdf')
  LOG ON(NAME='HOD201210_log', FILENAME='C:\OptionDB\HOD201210_log.ldf')
  FOR ATTACH WITH ENABLE_BROKER;
Go



I am also not sure if I can open sql server management studio since I moved the files before remapping or detaching.
0
Comment
Question by:jackjohnson44
6 Comments
 
LVL 25

Expert Comment

by:TempDBA
ID: 38729679
Yes you can. Are you seeing any issues?
0
 

Author Comment

by:jackjohnson44
ID: 38729716
Sorry, my question is, how do I make the databases point to the new files?  J drive vs C drive.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 38729925
You can open SSMS at any time. Did you stop SQL Server while moving the files? I'll hope so, else you should not have been able to move all files, as they were in use.

In SSMS, you should see the DB now as not active (suspect, damaged or something like that).
Switch it to Offline, Detach, Attach selecting the new MDF location, and correct all file locations shown as "not found".
You can do the same with SQL, of course, but since this is a one-timer (hopefully), I would prefer the "manual" method using the GUI.
0
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

Author Comment

by:jackjohnson44
ID: 38729942
Thanks, I did turn it off.  I was actually hoping for a script, I have a large number of those databases and going into the properties will take too long.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 38729977
alter database HOD201210 set offline;
sp_detach_db N'HOD201210'
CREATE DATABASE HOD201210 
  ON(NAME='HOD201210', FILENAME='J:\OptionDB\HOD201210.mdf') 
  LOG ON(NAME='HOD201210_log', FILENAME='J:\OptionDB\HOD201210_log.ldf') 
  FOR ATTACH WITH ENABLE_BROKER;

Open in new window

I'm not sure that a simple drop of the DB would help here (guess not), hence used offline/detach/attach as with the GUI.
0
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
ID: 38729980
As long as you haven't moved the system databases you should be able to do this:

ALTER DATABASE [your_db_name]
MODIFY FILE (NAME = Your_Data_File_Name, FILENAME = 'j:\directory\path\Data.mdf');
GO
ALTER DATABASE [your_log_name]
MODIFY FILE (NAME = Your_Log_File_Name, FILENAME = 'j:\directory\path\Log.mdf');
GO
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question