Cause: Because when I restore the database, there are other users who are using the database, so the above prompt appears.
Workaround:
1, set the database to work in single-user mode.
Setup method: Right-click on the database you want to restore, select Properties-> Options-> Status-> Restrict Access-> "single" On the right-click menu command. This is the menu command for SQLSERVER2005, other versions please find it yourself.
2, the use of SQL statements to kill all the processes that are using the database, I used to do a SQL Server operation of the small tool has written this function of SQL, posted for your reference:
DECLARE @dbname varchar (50)
Set @dbname = ' database name '
DECLARE @sql varchar (50)
Declare cs_result cursor local for select ' Kill ' +cast (spid as varchar) from sys.sysprocesses where Db_name (dbid) [Emai L protected]
Open Cs_result
FETCH NEXT from Cs_result to @sql
While @ @fetch_status =0
Begin
Execute (@sql)
FETCH NEXT from Cs_result to @sql
End
Close Cs_result
Deallocate Cs_result
The SQL statement uses the cursor to loop through all the processes that are using the database and kills the process with the kill command.
3. Using SQL statements, disconnect all user links and roll back all transactions, with the following SQL statements:
ALTER database [DB name]
SET OFFLINE with ROLLBACK IMMEDIATE
Note: When using method 2 and 3 o'clock, do not execute under the desired restored database, it is recommended to execute under the master database.
Error "The database is in use, so you cannot obtain exclusive access to the database" solution (GO)