patternsqlMajor
Copy & rename db so I can attach both copies
Viewed 0 times
cancopiesattachcopybothrename
Problem
I have a db called 'mysite_db' on a server and I have created a copy on my local machine which is also 'mysite_db'. I would like to attach the db (from my local machine) to the server which already has this db name. Meaning I have to change my local db's name (I guess I also need to change the physical file). I'm lost with how to do that. Can you please guide me?
My goal is to load the 2 databases on the server for example: 'mysite_db' and 'myNewSite_db'
My goal is to load the 2 databases on the server for example: 'mysite_db' and 'myNewSite_db'
Solution
If You want to use the same directory for your mdf\ldf files You will have to (physically) rename them.
(In My example I used D:\Data for mdf and E:\Logs for ldf, change the path before running the script)
(Assuming the original logical file name was "mysite_db")
Good luck,
Roi
- Detach files from local PC
- Rename mdf and ldf files
- Copy\Move the files to the server directory where the original Database is stored.
(In My example I used D:\Data for mdf and E:\Logs for ldf, change the path before running the script)
- Use this code:
USE [master]
GO
CREATE DATABASE [myNewSite_db] ON
( FILENAME = N'D:\Data\myNewSite_db.mdf' ),
( FILENAME = N'E:\Logs\myNewSite_db_log.ldf' )
FOR ATTACH
GO- If You want to change the logical file name to be the same as the physical one use:
(Assuming the original logical file name was "mysite_db")
USE [myNewSite_db]
GO
ALTER DATABASE [myNewSite_db]
MODIFY FILE (NAME=N'mysite_db', NEWNAME=N'myNewSite_db')
GO
ALTER DATABASE [myNewSite_db]
MODIFY FILE (NAME=N'mysite_db_log', NEWNAME=N'myNewSite_db_log')
GOGood luck,
Roi
Code Snippets
USE [master]
GO
CREATE DATABASE [myNewSite_db] ON
( FILENAME = N'D:\Data\myNewSite_db.mdf' ),
( FILENAME = N'E:\Logs\myNewSite_db_log.ldf' )
FOR ATTACH
GOUSE [myNewSite_db]
GO
ALTER DATABASE [myNewSite_db]
MODIFY FILE (NAME=N'mysite_db', NEWNAME=N'myNewSite_db')
GO
ALTER DATABASE [myNewSite_db]
MODIFY FILE (NAME=N'mysite_db_log', NEWNAME=N'myNewSite_db_log')
GOContext
StackExchange Database Administrators Q#24610, answer score: 20
Revisions (0)
No revisions yet.