还原数据库备份的时候会出现一个这样的错误提示:因为数据库正在使用,所以无法获得对数据库的独占访问权

解决方法是:修改一些数据库名称,执行一下sql代码,然后马上还原即可

use master

declare @dbname varchar(20)
set @dbname='dfd_Test'     ---这是数据库名称

declare @sql nvarchar(500)
declare @spid int--SPID 值是当用户进行连接时指派给该连接的一个唯一的整数
set @sql='declare getspid cursor for 
select spid from sysprocesses where dbid=db_id(
'''+@dbname+''')'
exec (@sql)
open getspid
fetch next from getspid into @spid
while @@fetch_status<>-1--如果FETCH 语句没有执行失败或此行不在结果集中。
begin
exec('kill '+@spid)--终止正常连接
fetch next from getspid into @spid
end
close getspid
deallocate getspid

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-10
  • 2021-05-31
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
猜你喜欢
  • 2021-09-13
  • 2021-12-26
相关资源
相似解决方案