Friday, December 30, 2011

Attach database failed : "Unable to open the physical file "....". Operation system error 5: "5(Access is Denied)". Microsoft SQL Server, Error: 5120)

If you receive this follow error while you are trying to attach you old or back-up database mdf file to your Microsoft SQL Server 2008, you can fix it by using the methods mentioned below.

Attach database failed .................
"Unable to open the physical file "....". Operation system error 5: "5(Access is Denied)". Microsoft SQL Server, Error: 5120)

There are several ways of fixing it. I looked for the solutions and found these 3 methods from http://blog.sqlauthority.com. Here, I am going to re-share them again.


Method 1. ----- Tested
CREATE DATABASE ygnonline ON
(FILENAME = N'D:\MyProjects\myapps\database\mydb_Data.mdf')
FOR ATTACH_REBUILD_LOG
GO
***The log file (.ldf) will be recreated.

Method 2.

EXEC sp_attach_single_file_db @dbname='TestDb',
@physname=N'D:\MyProjects\myapps\database\mydb_Data.mdf'
GO


Method 3.

CREATE DATABASE ygnonline ON
(FILENAME = N'D:\MyProjects\myapps\database\mydb_Data.mdf')
FOR ATTACH
GO


I never try myself on method 2 and 3 since the issue I have is fixed with the method 1. Please kindly be advised to check with your database expert before using above methods in live or production environment.
You can also go and read TechNet library of Microsoft for more details in this.
Wish you guys can fix all the bugs you got before coming of the new year!!! Good Luck!! Enjoy!!! All the best!!!!!!

2 comments:

  1. still i am getting the error i used all the 3 methods can any one solve this

    ReplyDelete
  2. Now users can quickly attach SQL database without facing any error with SQL recovery tool. http://www.sqlrecoverysoftware.net/blog/how-to-fix-sql-error-5171.html

    ReplyDelete