在还原数据库时,有时会提示因为数据库正在使用,所以无法获得对数据库的独占访问权!!
这时需要在还原数据库前先杀死正在使用数据库得线程.

该解决方案用到了系统表中的sysprocesses ,若要访问 sysprocesses,您必须定位到master 数据库。

如以下杀死正在使用'jcjq'数据库的线程:
use master
declare @dbname varchar(20)
set @dbname='数据库名称'

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

相关文章:

  • 2021-12-01
  • 2021-12-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2021-12-26
相关资源
相似解决方案