Sometimes this is a common error message that we encounter, when we try to restore a SQL database, which is being used by other users.
This can occur due to various reasons. But the most common incident is, users not closing the Management Studio’s query window after they have finished the query task.
There are few ways of resolving this and restore the database.
1. Find all the active connections, kill them all and restore the database
2. Get database to offline (And this will close all the opened connections to this database), bring it back to online and restore the database
Use the following script to find and kill all the opened connections to the database before restoring database.
Use the following code to take database offline and bring back to online so that all the active connections will be closed. And afterwards restore the database.