Error restoring sql sever database

Gabriel Bulla 0 Reputation points
2024-08-20T17:54:12.95+00:00

I have this error when i'm restoring a database

TITLE: Microsoft.SqlServer.Smo


Set offline failed for Database 'TasWebPruebas'.

For help, click: https://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=16.200.48044.0&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Set+offline+Database&LinkId=20476


ADDITIONAL INFORMATION:

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)


Changes to the state or options of database 'TasWebPruebas' cannot be made at this time. The database is in single-user mode, and a user is currently connected to it.

ALTER DATABASE statement failed. (Microsoft SQL Server, Error: 5064)

For help, click: https://docs.microsoft.com/sql/relational-databases/errors-events/mssqlserver-5064-database-engine-error


BUTTONS:

OK


SQL Server | Other
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 128.9K Reputation points MVP Volunteer Moderator
    2024-08-20T21:10:43.1666667+00:00

    What the error message says. The database is in single-user mode and some user is connected to the database. Could be yourself in an open query window...

    To find which process it might be, run sp_who2 and check the database column. If you can assume that it is not a critical process, use the KILL command to get rid of it.

    0 comments No comments

  2. CathyJi-MSFT 22,406 Reputation points Microsoft External Staff
    2024-08-21T02:30:04.1566667+00:00

    Hi @Gabriel Bulla ,

    Changes to the state or options of database 'TasWebPruebas' cannot be made at this time. The database is in single-user mode, and a user is currently connected to it. ALTER DATABASE statement failed. (Microsoft SQL Server, Error: 5064)

    Agree with Erland.  As this error message mentioned, this database is in single user mode and a user is currently connected to it. There is an active connection to this database. Please follow below steps to resolve this issue.

    1. Identify the active connection: Run the following command to see which session is connected to the database:
    EXEC sp_who2;
     
    

    Please check the DBName column to look for the session ID (SPID) that is connected to your database.

           21  

    2.Kill the active connection: Once you have identified the SPID, if possible, you can kill the connection using:

    KILL {session_id};
    

     Replace {session_id} with the actual SPID you found in the previous step.

    3.We suggest setting database to multi-user mode if there is no need to set it to single-user mode. We can using:

    USE master;
    ALTER DATABASE TasWebPruebas SET MULTI_USER WITH ROLLBACK IMMEDIATE;
    

     


    If the answer is helpful, please click "Accept Answer" and kindly upvote it.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.