Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Server: Add existing mdf with new Database-name

Posted on 2013-06-10
4
Medium Priority
?
542 Views
Last Modified: 2013-10-21
Hello!

we are using SQL Server  10.0.5500 on a SBS 2008 R2.
We made some backups of the *.mdf and *.ldf Files within the DATA-Folder of the SQL-Server. Now we need some of the data back and don't want to destroy the running system/database.
So we need to add the database to the server management studio and change the name so the import won't overwrite the existing (and running) database.

How do we do this?
Is there any possibility to create a new databasemodule? If so, we can add the database to the new one and create some sql-statements to change/add the running database.

Any help ist appreceated.
Thanks
Jens

P.S. We also could create a VM and import the data to the VM-system. After that we can create the sql-actions, but this seems to be a lot more work ...
0
Comment
Question by:loosain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 8

Assisted Solution

by:didnthaveaname
didnthaveaname earned 668 total points
ID: 39235200
Sounds like you want to do a CREATE DATABASE with the FOR ATTACH clause

http://msdn.microsoft.com/en-us/library/ms176061%28v=sql.100%29.aspx

It is as straightforward as:

create database dbName_BACKUP
on
   ( filename = 'path to mdf' ),
   ( filename = 'path to first ndf' ),
   ...,
   ( filename = 'path to last ndf' ),
   ( filename = 'path to first ldf' ),
   ...,
   ( filename = 'path to last ldf' )
for attach;

Open in new window


Hope that helps!  As an aside, I would move the backups of the mdf/ndf/ldf's that you guys made into a subfolder to prevent confusion.  And once you create the new database (provided it's in the same instance), you can move data from one to the other, just be careful =)  I would, personally, recommend that the direction of data flow always be towards the copy and when you have it to where you want it, detach both dbs, and then create the production DB from the modified files.
0
 
LVL 21

Assisted Solution

by:Alpesh Patel
Alpesh Patel earned 664 total points
ID: 39237384
Just attach database using the SSMS and give a new name to Database.


sp_attach_db 'DBName', 'DBfilePAth'
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 668 total points
ID: 39238733
CREATE DATABASE OLD_DB_NAME__TEMP_RESTORE
ON
    --must specify *all* data files for the db (hopefully only 1);
    --the "NAME=" value can be anything you want that is a valid name,
    --    except of course that two files on the same db can't have the same NAME = value
    ( NAME = data_file1, FILENAME = 'd:\full\path\to\file\DATA\<data_filename>.mdf' ),
    --must specify *all* log files for the db (hopefully only 1)
    --the "NAME=" value can be anything you want that is a valid name
    --    except of course that two files on the same db can't have the same NAME = value
    ( NAME = log_file1, FILENAME = 'd:\full\path\to\file\DATA\<log_filename>.ldf' )
FOR ATTACH


Always use a command, such as above for this, and not the GUI, because:

1) the GUI can be flaky and unreliable for this
2) with the command, you can call the command back up later and see exactly what you specified
0
 

Author Closing Comment

by:loosain
ID: 39589350
thx
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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 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