Sql-server – Copy database removed source – Cannot acces mdf file

sql serversql-server-2017ssms

I attempted to copy a database, using Microsoft SQL Server Management Studio (SSMS), but right-clicking and following the wizard. I wanted to target server to be the same as the source, so I added "_new" to the database name.

The process failed (unfortunately I missed the error message) and now the source database is missing as well. I found another post on the same topic, which suggests locating and atttaching the .mdf file.

However, when I try to attach the mdf file, I get the following error meesage:

Failed to retrieve data for this request. (Microsoft.SqlServer.Management.Sdk.Sfc)

Error when I try to attach the mdf file.

Luckily this was just a test database, so I didn't lose any data, that couldn't be easily reproduced. But is there a way to recover from this? If it ever happened on a production database, I would like to be prepared.

Best Answer

The process failed (unfortunately I missed the error message) and now the source database is missing as well. I found another post on the same topic, which suggests locating and atttaching the .mdf file.

If the database is no longer listed on the instance, but you can still see the .mdf file, it somehow got detached during this first unsuccessful attempt. The screenshot error says:

CREATE FILE encountered operating system error 5(Access denied.)

The error is related to security and Microsoft states something about security changes during the process of detaching or attaching a database:

File access permissions are set during a number of database operations, including detaching or attaching a database.

In order to fix it, check the permissions of the .mdf file and if you confirm it has really been changed, alter them to allow you to execute the attach procedure or run the attach procedure with the same login that is now the owner of the .mdf file.


DANIEL HUTMACHER described this behaviour very well in his article Detaching a database also alters file permissions and his conclusions are:

  • Detaching a database when logged on with a Windows account will change the file permissions, restricting SQL Server’s service account and only allowing your user to access the files.
  • Detaching a database as “sa” or a SQL Server account will leave the files’ permission sets unchanged.
  • If you’re connected as “sa” or another SQL Server account, SQL Server’s service account needs permissions on any database files you
    want to attach.
  • If you’re connected as a Windows login, you need sufficient permissions on the files to attach them.