Home » “Access denied” when attaching a database via SSMS

“Access denied” when attaching a database via SSMS

by Vlad Drumea
0 comment

A few ways of fixing the access denied error you’re attaching a database to an instance, either as a result of:

  • SQL Server reinstall
  • Just wanting to move or copy the mdf and ldf files between servers (you should use backup & restore instead if you have backups available)
  • The original VM dying and the Server team mounting the SQL Server disk(s) to another VM for recovery purposes

In certain situations, you’ll run into the “CREATE FILE encountered operating system error 5(Access is denied.)” error message in SSMS even though you’ve granted full control over the files to the SQL Server service account via icacls.

OS access denied attaching database via SSMS Failed to retrieve data for this request. (Microsoft.SqlServer.Management.Sdk.Sfc) ADDITIONAL INFORMATION: An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo) CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file 'E:\VSQL\Data\AdventureWorks2017.mdf'. (Microsoft SQL Server, Error: 5123)
OS access denied error when attaching a database via SSMS

Full error text:
Failed to retrieve data for this request. (Microsoft.SqlServer.Management.Sdk.Sfc)
ADDITIONAL INFORMATION:
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file 'E:\VSQL\Data\AdventureWorks2017.mdf'. (Microsoft SQL Server, Error: 5123)

Cause

This error is unrelated to the permissions that the SQL Server service account has over the files, so this implies that you’ve already handled that part by running icacls in an administrative command prompt:

Or directly on the files themselves:

This happens when attaching the database files via the GUI and your own user account (either local or AD) doesn’t have explicit full control permissions over the files. Even if your user account is a member of the local Administrators group, because SSMS is not running with elevated privileges, it’s just running as your normal user account.

Fix

There are a few ways of fixing this (each bullet point represents a complete fix for this error):

  • Open SSMS locally (on the host of the instance to which you’re trying to attach the files) as Administrator (right click on SSMS > Run as Administrator) and the GUI will no longer error out due to it running with elevated privileges
  • Use the icacls example above for granting full control over the mdf and ldf files only, but replace the service account part with your account
  • Although this option no longer relies on SSMS, it’s still valid – open Command Prompt as Administrator and use SQLCMD and T-SQL to attach the database
In cmd opened as Admin: C:\Windows\system32>sqlcmd -S localhost\vsql2019 -E 1> USE [master] 2> GO Changed database context to 'master'. 1> CREATE DATABASE [TEST] ON 2> (FILENAME = N'E:\VSQL\Data\TEST.mdf'), 3> (FILENAME = N'E:\VSQL\TLog\TEST_log.ldf') 4> FOR ATTACH 5> GO 1> SELECT [name], [state_desc] 2> FROM sys.databases 3> WHERE [name] = N'TEST'; access denied attaching database
Attaching a database via CMD to avoid the access denied error

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.