Error while restoring a backup - Execute permission denied
Description:
When trying to restore a backup into an Application Suite 2017.Winter.1 or newer, the operation could fail.
Logs contained in BillyBlues.Core.Wizard folder show the following error:
System.Data.SqlClient.SqlException : The EXECUTE permission was denied on the object 'sp_detach_db', database 'mssqlsystemresource', schema 'sys'.
Stacktrace
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
at ApplicationSuite.Data.Database.QueryExecutor.ExecuteQuery(String sql, SqlParameter[] parameters, TimeSpan timeout)
at ApplicationSuite.Data.Database.QueryExecutor.ExecuteQuery(String sql, SqlParameter[] parameters)
at ApplicationSuite.Data.Database.QueryExecutor.ExecuteQuery(String sql)
at ApplicationSuite.Data.Database.DatabaseSchema.Rename(String newName)}
Cause:
This error happens because the user used to connect to SQL Server is missing a required permission, i.e. the permission to execute the sp_detach_db
stored procedure.
Since SQL Server 2008 this permission is granted by default (on a fresh install) to every user that can connect to the SQL Server instance, but earlier versions could have different default values (and upgrading an older version of SQL Server to a newer one keeps the old default settings).
To verify that the user has this permission you can log into SQL Server with that same user and run this script:
SELECT
'Can execute ''sp_detach_db''' AS [Requirement],
CASE WHEN HAS_PERMS_BY_NAME('sp_detach_db', 'OBJECT', 'EXECUTE') = 1 THEN 'OK' ELSE 'MISSING' END AS [Status]
Solution:
Grant the user the permission to execute the sp_detach_db
stored procedure.
As an example you can run the following script as a SQL administrator, and replacing imagicleUser
with the actual username:
USE [master]
CREATE USER [imagicleUser] FOR LOGIN [imagicleUser]
GRANT EXECUTE ON sp_detach_db TO [imagicleUser]