gogoWebsite

Solution to SQLServer database restore without exclusive access permissions

Updated to 2 days ago

Solution:
1. Set the database to work in single-user mode.
Setting method: Right-click on the database that needs to be restored, and select "Properties"->"Options"->"Status"->"Restrict access"->"Single" on the right-click menu command. This is the menu command of SQLSERVER2005. Please search for other versions yourself.

2. Use SQL statements to kill all processes that are using the database. I used to write SQL for this function when I was working on a SQL SERVER operation tool. I posted it 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(50)) from 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

This SQL statement uses a cursor to loop all processes that are using the database and kills the process through the kill command.

3. Use SQL statements to disconnect all user links and roll back all transactions. The specific SQL statements are as follows:
ALTER DATABASE [Database Name]
SET OFFLINE WITH ROLLBACK IMMEDIATE

4. Delete the original database and only check the existing link of the block

View existing code for connecting users and deleting links in the database:

SELECT spid FROM

[Master].[dbo].[SYSPROCESSES] WHERE [DBID] IN ( SELECT

[DBID]

FROM

[Master].[dbo].[SYSDATABASES]

WHERE

NAME=’publicserviceplatform’

)

kill 51;

kill 52;

kill 63;

kill 64;

–kill 69;

kill 70;

kill 71;

kill 85;