When restoring the database today, the system prompts that the database is in use, so it cannot obtain exclusive access to the database. No matter whether I restart the database or restart the computer, the problem cannot be solved, after several attempts, the problem was finally solved. The cause of the problem and the solution are written. If there is anything wrong, you are welcome to raise it. Trigger original
Today, when restoring the database, the system prompts "because the database is in use, it cannot obtain exclusive access to the database". No matter whether I restart the database or restart the computer, the problem cannot be solved, after several attempts, the problem was finally solved. The cause of the problem and the solution are written. If there is anything wrong, you are welcome to raise it. Trigger original
Today, when restoring the database, the system prompts "because the database is in use and the Hong Kong virtual host cannot obtain exclusive access to the database", whether I restart the database or the computer, server leasing in Hong Kong cannot solve the problem. After several attempts, the problem was finally solved. The cause of the problem and the solution are written. If there is anything wrong, you are welcome to raise it.
Cause: When I restore the database, other users are using the database, so the above prompt will appear.
There are three solutions:
First, use the cursor to loop through all processes that are using the database and kill the process.
Declare @ dbname varchar (50)
Set @ dbname = 'database name'
Declare @ SQL varchar (50)
Declare cs_result cursor local for select 'Kill '+ cast (spid as varchar (50) from sys. sysprocesses where db_name (dbid) = @ dbname
Open cs_result
Fetch next from cs_result into @ SQL
While @ fetch_status = 0
Begin
Execute (@ SQL)
Fetch next from cs_result into @ SQL
End
Close cs_result
Deallocate cs_result
Note: Do not run the command in the database to be restored. We recommend that you run the command in the master database.
Second, use SQL statements to disconnect all users and roll back all transactions.
Alter database [DATABASE name]
SET OFFLINE WITH ROLLBACK IMMEDIATE
Note: Do not run the command in the database to be restored. We recommend that you run the command in the master database.
Third: Set the database to work in single-user mode
Right-click the database to be restored, and select Hong Kong Space, right-click the menu and choose "properties"> "option"> "status"> "Restrict Access"> "Single ". This is the menu command of SQLSERVER2005. For other versions, find them by yourself.