ALTER DATABASE failed because a lock could not be placed on database

后端 未结 10 2226
感情败类
感情败类 2020-12-12 09:52

I need to restart a database because some processes are not working. My plan is to take it offline and back online again.

I am trying to do this in Sql Server Manage

相关标签:
10条回答
  • 2020-12-12 10:12

    I will add this here in case someone will be as lucky as me.

    When reviewing the sp_who2 list of processes note the processes that run not only for the effected database but also for master. In my case the issue that was blocking the database was related to a stored procedure that started a xp_cmdshell.

    Check if you have any processes in KILL/RollBack state for master database

    SELECT *
    FROM sys.sysprocesses
    WHERE cmd = 'KILLED/ROLLBACK'
    

    If you have the same issue, just the KILL command will probably not help. You can restarted the SQL server, or better way is to find the cmd.exe under windows processes on SQL server OS and kill it.

    0 讨论(0)
  • 2020-12-12 10:12

    In SQL Management Studio, go to Security -> Logins and double click your Login. Choose Server Roles from the left column, and verify that sysadmin is checked.

    In my case, I was logged in on an account without that privilege.

    HTH!

    0 讨论(0)
  • 2020-12-12 10:12

    Killing the process ID worked nicely for me. When running "EXEC sp_who2" Command over a new query window... and filter the results for the "busy" database , Killing the processes with "KILL " command managed to do the trick. After that all worked again.

    0 讨论(0)
  • 2020-12-12 10:19

    After you get the error, run

    EXEC sp_who2
    

    Look for the database in the list. It's possible that a connection was not terminated. If you find any connections to the database, run

    KILL <SPID>
    

    where <SPID> is the SPID for the sessions that are connected to the database.

    Try your script after all connections to the database are removed.

    Unfortunately, I don't have a reason why you're seeing the problem, but here is a link that shows that the problem has occurred elsewhere.

    http://www.geakeit.co.uk/2010/12/11/sql-take-offline-fails-alter-database-failed-because-a-lock-could-not-error-5061/

    0 讨论(0)
  • 2020-12-12 10:21

    Just to add my two cents. I've put myself into the same situation, while searching the minimum required privileges of a db login to run successfully the statement:

    ALTER DATABASE ... SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    

    It seems that the ALTER statement completes successfully, when executed with a sysadmin login, but it requires the connections cleanup part, when executed under a login which has "only" limited permissions like:

    ALTER ANY DATABASE
    

    P.S. I've spent hours trying to figure out why the "ALTER DATABASE.." does not work when executed under a login that has dbcreator role + ALTER ANY DATABASE privileges. Here's my MSDN thread!

    0 讨论(0)
  • 2020-12-12 10:23

    I know this is an old post but I recently ran into a very similar problem. Unfortunately I wasn't able to use any of the alter database commands because an exclusive lock couldn't be placed. But I was never able to find an open connection to the db. I eventually had to forcefully delete the health state of the database to force it into a restoring state instead of in recovery.

    0 讨论(0)
提交回复
热议问题