An error occurred when attaching the database(s). Click the hyperlink in the Message column for details [closed]
Asked Answered
J

14

27

i am trying to attach external database named ( HaseebProject.mdf) but every time i got an error " An error occurred when attaching the database(s). Click the hyperlink in the Message column for details." What'sthe error is there..?? even there is any error message in message field as shown.

enter image description here

When i click on add button to add database there are two paths in directory for database. i have tried with both but he same error.

enter image description here

Josejosee answered 8/5, 2016 at 14:56 Comment(0)
S
69

Try running SQL Server Management Studio as administrator

Showalter answered 25/9, 2016 at 10:15 Comment(3)
This did not work for me. This doesn't seem to work if the mdf file is in a shared folder from a host machine.Lindsy
This worked for me. Maybe you have corrupt the .mdf file?Dramamine
This worked for me. Sometimes it's the simplest of reasons.Anastasiaanastasie
P
14

All of the answers I've seen so far are possible suggestions of what could be the issue, but it doesn't necessarily help you identify the issue that's preventing you from attaching this database.

The error message you are referring to is actually pointing you to a more specific error message for your case. This more specific error message is located in the Message column of the "Attach Database" window that you've already opened (you may just need to scroll to the right of the "Databases to attach" section and click on the message to see it in its entirety.

See circled area

Popp answered 8/8, 2019 at 22:7 Comment(3)
I agree with what Ryan Harris has written in the comment. In my case the error was that the database I was trying to attach already existed. Then I used a .bak file and restored the database. It worked.Procreant
Thanks! This helped my solve my error on the .mdf file being compressed by Windows. Check my answer below.Perihelion
The most helpful answer. My issue was that log files were located in the next folder, not the same oneSpode
M
8

I had this problem as well, I did right click on (.mdf and .ldf) the file then on the security tab, click on Edit.

In a new window find the users and set permission to full control. If you can't find users click on:

add button-->advanced-->Find now-->find users 

and then click on Ok. if it doesn't work you can backup from your database and restore it instead of attaching. I have done it and it works.But before restoring make sure that your DB (.mdf and .ldf) shouldn't exist in SQL server database directory before restoring (it's up to your SQL server installation path in my case -->C:\Program Files\Microsoft SQL Server\MSSQL12.SQL2014\MSSQL\DATA) otherwise you will face to an error

Mariellemariellen answered 10/10, 2017 at 10:31 Comment(1)
in my case the user was added... I only had to give Full Control permissions. Now it is workingGem
P
3
  1. Navigate to the directory containing the database and open the properties for the folder.
  2. Go to the Security tab, Edit permissions, Add User.
  3. In Locations, select the location that is hosting the server.
  4. In object names, Enter NT Service\MSSQL$<Instance-Name> (e.g. NT Service\MSSQL$MSSQLSERVER).
  5. Set Permissions to full control and save.

This will give your SQL Server instance full permissions for the target directory.

Polyester answered 25/11, 2017 at 0:42 Comment(0)
E
1
  1. Right-click on the MyDataBase.MDF file
  2. Select Properties -> Go to Security tab -> Select Edit -> Select Add -> Select Advanced -> Select Find Now -> Selecte User Administrator & User Everyone
  3. Set permissions to full control for both
  4. Click OK
  5. Clear MyDataBase_log.LDF file
  6. Copy MyDataBase.MDF file to this location:

    (C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA)

  7. Run SQL Server Management Studio as an administrator

  8. Attach MyDataBase.MDF file from this address:

    (C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA)

Eddy answered 10/8, 2018 at 5:23 Comment(0)
T
1

In my case I changed the main database file names so I had to select them again using button in "Current File Path" column.

enter image description here

Thorley answered 26/2, 2020 at 15:31 Comment(0)
H
1

If you already have a database with the same name, delete it before you try to attach the .mdf file. My issue was that they were named the same.

Ryan Harris mentioned looking at the error message, this helped me a lot. So look at that first, you might have a different issue.

SQL message

Hoyos answered 5/8, 2021 at 20:58 Comment(0)
A
0

I had the same issue. The problem was the disk which I was transferring the database file. So if you are copying the database file from one computer to another computer make sure the removable disk is not corrupted. Always try a second removable disk. And also don't forget to run SSMS as administrator.

Albrecht answered 3/5, 2018 at 16:30 Comment(0)
R
0

Use this command to attache db from .mdf file

USE [master]
GO
CREATE DATABASE [SchoolDB] ON 
( FILENAME = N'C:\SchoolDB.mdf' )
FOR ATTACH
GO
Raiment answered 10/8, 2019 at 12:49 Comment(0)
P
0

I had the same issue. After I expanded the error message, I found that it was saying the .MDF file was compressed.

I googled the issue and found this resource that solved my issue. https://blogs.msdn.microsoft.com/sqlblog/2006/10/02/sql-server-databases-are-not-supported-on-compressed-volumes/

If your error is similar to this:

Create failed for Database 'Compressed_DB'.  (Microsoft.SqlServer.Smo)

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

The file "E:Compressed_DB.mdf" is compressed but does not reside in a read-only database or filegroup. The file must be decompressed.

CREATE DATABASE failed. Some file names listed could not be created. Check related errors. (Microsoft SQL Server, Error: 5118)

Then you can right click the folder in which the .MDF file resides, click "Properties". Click "Advanced" in the General tab, and uncheck "Compress contents to save disk space"

This solved the error I was getting, so try it out and attach your .MDF again.

Perihelion answered 14/11, 2019 at 22:38 Comment(1)
What do you mean "After I expanded the error message"? There doesn't seem to be anything to expand.Giorgi
T
0

Resolution:- Just give permission to database folder and attached mdf file then.

Trattoria answered 8/6, 2022 at 8:45 Comment(0)
C
0

check version of the sql server software and the sql software that mdf was created. must be the same versions.

Corinthian answered 28/8, 2022 at 12:23 Comment(0)
S
0

enter image description here

you should be accept allow security MDFfile

Sydelle answered 9/1, 2024 at 6:7 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Kulak
F
-1

Give full control to both the .mdf file and the .ldf file

enter image description here

Fantasm answered 13/2, 2018 at 6:49 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.