Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Hi I have mdf and ldf files from a different SQL Server install from a machine i dont have access to now, and would like attach them to my current install to get at the data. When I try this, I get an error:

Cannot attach a database with the same name as an existing database.  (Microsoft.SqlServer.Smo)

This is because the mdf is named msdbdata, just like my current install. I don't know if there is even a way to rename this database now or what I can do. Any help would be appreciated. Thanks!

share|improve this question
    
Well you really wouldn't be moving the MSDB database from one server to another server except in very specific circumstances, so I have to ask why you're trying to do this? –  joeqwerty Jun 3 '11 at 19:22
    
I've had to do it to restore agent jobs, for example. –  squillman Jun 3 '11 at 19:27
    
found myself having to do this when my laptop crashed and i moved the drive to another machine - obviously not a production env. –  Simon May 1 '13 at 0:02
add comment

2 Answers

up vote 6 down vote accepted

You can rename the file, or put it in a different folder than your existing file, and then make sure to change the name of the database when you reattach it.

How are you trying to reattach it? Via SQL Server Management Studio or through tsql?

First, copy your mdf and ldf files to something new (name them relative to a new database you'll attach them as). Then in the Attach Databases screen:

  • Enter a new name for a (nonexisting) database in the Attach As field in the top frame
  • In the bottom frame, browse to both the new mdf file and the new ldf file you just created.

You should then get the new database attached.

share|improve this answer
    
Thanks for the response. I am using Management Studio 2005. I don't know how to attach it to a different place in Management studio than the Databases folder, which is where my error comes in. –  Drew Jun 3 '11 at 18:51
    
@Drew see my edit –  squillman Jun 3 '11 at 19:01
    
it worked! you are the man, squillman :D –  Drew Jun 3 '11 at 19:18
    
@Drew Excellent :) –  squillman Jun 3 '11 at 19:27
add comment

This error can also occur when you're being stupid and trying to attach a database you just removed from a different location when you're not paying attention and choose the wrong file. Oops

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.