1

I have attached x_Data.mdf and x_Log.ldf file successfully with SQL Server, to replace the same file I deleted and tried to attach with new file having same name. But it is throwing below error statement.

Can anybody please shed some light on this?

Msg 5173, Level 16, State 2, Line 1
One or more files do not match the primary file of the database. If you are attempting to attach a database, retry the operation with the correct files. If this is an existing database, the file may be corrupted and should be restored from a backup.

  • I assume the database therefore was not attached. If you're doing this in the "Attach" dialog box, can you use its "Script" option to see a textual version of the "attach" operation? – Robert Carnegie Jul 24 '17 at 11:22
  • I do not understand your question. You attached successfully first? Deleted files and created new file with same name? – SqlWorldWide Jul 24 '17 at 11:34
  • @user131199, did you try to create through this script CREATE DATABASE [name] ON (FILENAME = [path to mdf]) LOG ON (FILENAME = [path to ldf]) FOR ATTACH REBUILD LOG – Md Haidar Ali Khan Jul 24 '17 at 11:38
  • @SqlWorldWide i have got new .mdf file with updated data. – user131199 Jul 24 '17 at 11:47
  • @MdHaidarAliKhan yes and getting the same error – user131199 Jul 25 '17 at 6:45
2

You should check the real database name + files it consists of by executing dbcc checkprimaryfile on your mdf file

as described here: SQL SERVER – FIX – Error: One or more files do not match the primary file of the database

|improve this answer|||||
  • 2
    I'm not on 2008 yet (!) but I believe that the "primary" file (MDF) contains within it the names of other files that should be in the database; if you "attach" only the MDF then it automatically tries to attach any NDF and LDF files that were previously involved. This is overridden by explicitly stating all file names, ideally in the original order but that may not matter because the other files will identify themselves too. Getting that wrong may have produced your error message. At a pinch you may be allowed to re-create a new empty LDF instead of attaching it. – Robert Carnegie Jul 24 '17 at 11:27
  • @RobertCarnegie thanks! you save my day. Re-created a new empty .ldf file and its working fine [link]stackoverflow.com/questions/926096/… – user131199 Jul 24 '17 at 11:44
  • @RobertCarnegie You should post your comment as an answer – John Eisbrener Jul 25 '17 at 14:09
  • I think I didn't provide an answer, but a useful hint - "u" needed to use the method that (s)he found elsewhere to get a database working when you only have the MDF file. I didn't understand from the original description that that was the situation. Perhaps my fault, or perhaps a case of "you know what you meant", and maybe English as a second language. To work around that problem, it can be good to use more words, and more details. – Robert Carnegie Jul 25 '17 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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