Solved

How to rename MSSQL DB Permanently After Restore?

Posted on 2013-11-05
5
293 Views
Last Modified: 2013-11-10
I am splitting my database into multiple databases and trying to maintain existing schemas and data.
I need to RESTORE an MSSQL  database 2 or more times and rename it permanently.
I need the names of the physical files to change and during this rename process.
How is this best achieved?
SQL2012
0
Comment
Question by:XGIS
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:mikeyd234
Comment Utility
There are several ways to do the rename, however to rename the physical database files at operating system level you will have to take the database offline

1. Use SSMS to take the database Offline (right-click on Database, select Tasks, Take Offline), change the name of the files at the OS level and then Bring it Online.

2. You could Detach the database, rename the files and then Attach the database pointing to the renamed files to do so.

3. You could Backup the database and then restore, changing the file location during the restore process.

4. using T SQL

ALTER DATABASE databaseName SET OFFLINE
GO


ALTER DATABASE databaseNAme MODIFY FILE (NAME =db, FILENAME = 'C:\Program
Files\Microsoft SQL Server\MSSQL.2\MSSQL\Data\db.mdf')
GO


--if changing log file name
ALTER DATABASE  databaseNAme MODIFY FILE (NAME = db_log, FILENAME =
'C:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Data\db.ldf')
GO


ALTER DATABASE databaseName SET ONLINE
GO


Easier though to just take it offline and use windows explorer to browse to the DB mdf files and rename. (option 1)

Thanks
0
 
LVL 7

Author Comment

by:XGIS
Comment Utility
Hello mikeyd234... thats the same page as I am on.  I will delete it all and try it again from restore.  It wont come back online right now because it is still looking for old paths. etc
0
 
LVL 3

Expert Comment

by:mikeyd234
Comment Utility
Try detach the database, then reattach specifying the new file names
0
 
LVL 7

Accepted Solution

by:
XGIS earned 0 total points
Comment Utility
My solution was to Restore and Rename the database to a new name then.  Then click on files and change the names of all the individual db and log files prior to clicking OK. I now have 2 databases running independently.
0
 
LVL 7

Author Closing Comment

by:XGIS
Comment Utility
Hello mikey, pls note I tried attach reattach also.  As soon as it goes live things get complex.   The rename at restore is the quickest and cleanest solution I could identify.  Thankyou for your input.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now