I have a SQL agent job whereby we do a restore of a full backup of production data on a nightly basis. We are setting up a new server and when I attempt to run the job on the new server I receive an error on the job of
3701 - Cannot drop database because it does not exist or you do not have permission.
This is confounding me because we are using the dbo user to accomplish this step and the database is listed in sys.databases catalog view. I have tried altering the database to put it into single_user to no avail.
The first step of the job kills all connections and that passes. I am not sure where this error is coming from because everything looks in accord to me. We can also drop the database manually through logging in as the sa.
Related
I have setup the RDS instance for SQL Server 2019 SE. Option group also created with major version=15. SQL Server backup and restore option is added along with S3 settings and IAM role. The option group is also attached with instance.
I executed the command;
exec msdb.dbo.rds_restore_database #restore_db_name='MyDatabaseOnRDS_Staging',
#s3_arn_to_restore_from='arn:aws:s3:::MyBucketName/MyDatabaseOnRDS_Staging.bak';
The above statement created the task which is visible in;
exec msdb.dbo.rds_task_status;
The % complete goes to 100%. But then it shows ERROR in 'lifecycle' column. When I check the details in task_info column, it shows the error at the end;
The proposed new database owner is already a user or aliased in the database. Changed database context to 'master'.
What am I doing wrong?
Ok, the issue was sa user. It was added as a db user. It was in disabled state, but for some reason AWS RDS was not allowing rdsa to be the DB owner of the DB in restore as sa was the part of db users.
I did to following steps;
Changed the sa schema owner to dbo
Removed sa user (just user and not the sa schema) from the DB Users.
Took the backup, copied the backup to S3 and restored it.
You're attempting to restore the database from within a connection to that database. Change the context you're executing the restore operation from to the master database, as the error suggested.
I have sysadmin privileges on a SQL Server (2014). When I query the table sys.databases from one of my user database (test_db) I get the full list of databases (13 of them inclusive of system databases) that are actually on the server.
But when I execute as 'dbo', some kind of permission deny seems to be applied and the full list of databases don't appear. Neither am I able to see the list of databases nor I am not able to query objects within them as well. For example: I am not able to query msdb.dbo.sysjobs.
Here is what I am running on my user database called "test_db":
EXECUTE AS user = 'dbo'
Then I do the following query:
select * from sys.databases
I see only 3 databases (out of 13 databases) as shown below (master, tempdb, test_db):
I checked the login and principal identifications after the "execute as" and below you can see what those are:
After revert, it executes as myself and I am able to see the full list of DBs back again.
As user 'dbo' which is the database principal for server principal 'sa' it is supposed to have full db_owner access across all databases on the server, but unfortunately it is not so, in this case.
One additional information here is, "test_db" gets restored from a prod server backup every 2 weeks.
Can you help me to figure out why this is happening, please?
Edit: sorry i meant server principal 'sa' not login.
I have a stored procedure that I can execute in SSMS with a non domain SQL Server user.
This stored procedure selects data from tables in one database (DB1) truncates and selects into a table in DB2.
The user has datareader,datawriter and dbowner for both databases.
Problem:
When I execute the stored procedure via SS Agent with execute as the user I get the following error
The server principal [user] is not able to access the database [DB1]
under the current security context.
Actions taken So far:
I have tried to resolve this so far by:
Turning on db chaining for both databases
Deleted the user from DB1 and added again
Checked using EXEC sp_change_users_login #Action=’Report’ to see if user orphaned. As this is a database that is a restore of a live one. However I added the user after the restore. The user was not listed as orphaned
A possible workaround if you don't want to have the owner be sa is to have the user be a member of msdb and grant the the SQLAgentOperatorRole in msdb. See if that works.
But to be honest, either use sa or a dedicated service account with enough permissions. It's better if the job runs under that context.
I tried to migrate a SQL Server database by Export Data-tier Application (.bacpac file) from an Amazon RDS instance to other, but import didn't succeed. So now I want to delete the database (which is empty), when I try to:
DROP DATABASE mydatabase;
I get the error:
Cannot drop the database 'mydatabase', because it does not exist or
you do not have permission
Some context:
I've tried using SQL Server Management Studio, and choosing close connections: same error.
I'm logged as master user.
I can create and drop other databases, but not this one.
I just have these effective permissions on this database: CONNECT, SHOWPLAN, VIEW DATABASE STATE, VIEW DEFINITION (don't know why or how is this possible).
Any help is greatly appreciated!
I ran into this same issue. After trying to restore a database via SSMS using a .bacpac, it fails and leaves you with a database that you appear to not have permissions to drop.
A workaround, is to use the rdsadmin rename function to rename it to something else, which then seems to fix the permission issue and allows you to drop it.
EXEC rdsadmin.dbo.rds_modify_db_name N'<OldName>', N'<NewName>'
Then just drop the DB. Hope that helps someone else in the same predicament.
This is the answer for an old thread but who knows, it might help someone having the same issue.
I ran into the same problem, but in my case, my database was in an offline mode. If the database is in offline mode, it won't allow you to drop it with the drop command. first, you should bring the database back online by running this sp and then execute the drop table command.
EXEC rdsadmin.dbo.rds_set_database_online databasename
If your database is in a Multi-AZ deployment, then you need to run this command to drop those databases:
EXECUTE msdb.dbo.rds_drop_database N'DBName'
Sounds like your not a member of the correct role.
https://msdn.microsoft.com/en-us/library/ee240822.aspx
Permissions
A DAC can only be deleted by members of the sysadmin or serveradmin fixed server roles, or by the database owner. The built-in SQL Server system administrator account named sa can also launch the wizard.
https://msdn.microsoft.com/en-us/library/ms178613.aspx
Permissions
SQL Server - Requires the CONTROL permission on the database, or ALTER ANY DATABASE permission, or membership in the db_owner fixed database role.
Azure SQL Database - Only the server-level principal login (created by the provisioning process) or members of the dbmanager database role can drop a database.
Parallel Data Warehouse - Requires the CONTROL permission on the database, or ALTER ANY DATABASE permission, or membership in the db_owner fixed database role.
I executed the task Take offline of a SQL Server 2008 R2 database.
I cant bring it online!
I tried with
USE master;
GO
ALTER DATABASE [DBNAME] SET ONLINE
I get an error.
User does not have permission to alter database 'DBNAME', the database
does not exist, or the database is not in a state that allows access
checks.
I also tried using the task Bring online and I get the exact same error.
Can anyone help me asap?
I think you're going to need to login with the SA account, or some other account with sysadmin privileges, and then run your code to put it back online. While you're in there, add sysadmin to your account, too. If you don't own the database, you may need to talk to someone else to get the SA password, or ask them to do it for you. Unless you don't have the SA password or it's been dumbed down for security reasons.
Your error is too generic to be usable. Do you actually have the rights to alter the database (I guess you do if you managed to bring it offline)? Can you access teh SQL logs (accessible in the tree via Management > SQL Server logs)? Are you sure the user who is executing the script is the one you expect?
Also, you can try any of the following
* restart the service then retry
* Use the mouse GUI o bring it online (right click on the DB, Tasks, Bring Online)
Had same problem, same error. Even logged on as SA and returned same error. On both problem database the owner was not SA.
Solved by attaching the database. This gives you the opportunity to rename the database was well assign an owner. Assigned owner as SA.
Detached failed database the renamed the newly attached database to the original name.
A lesson in always give SA ownership of new databases.