Solved

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

Posted on 2012-12-29
6
701 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

831 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