Sqlserver-dba.com

sp_detach_db causing loss of permissions

I was using sp_detach_db to detach a database and then attempt to transfer with another DOS command. I was receiving a Access denied and couldn't figure why . Investigating closer revealed a change of permissions when using the sp_detach_db.

This was only in the situation of being a windows principal. In other words when attempting to detach a db the system resets the ACL of the file giving the windows user full control, with all permissions being removed

The workaround is to use the security context of a SQL  Logon, an example would be :


 EXECUTE AS LOGIN='sqlLogon'

 exec sp_detach_db 'my_db'

 exec xp_cmdshell 'COPY "E:\mssqlserver\mssql$inst1\data\my_db.mdf" "E:\mssqlserver\mssql$inst1\data\my_db_copy.mdf"'


 REVERT
 GO

Source:Jack Vamvas (http://www.sqlserver-dba.com)
Author: Jack Vamvas (http://www.sqlserver-dba.com)

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact