sql server 恢复数据库时提示有其他用户连接,怎么办?

今天在用sql server 2008 恢复数据库时提示有其他用户连接,无法继续恢复了。很头疼,呼唤百度帮忙。结果查到了如下的sql 语句,可以结束用户连接。


begin
declare @spid varchar(20)
declare @dbname varchar(20)
select @dbname='databasename'--换成要操作的数据库名称

declare #spid cursor for
select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)
open #spid
fetch next from #spid into @spid
while @@fetch_status=0
begin
exec('kill '+@spid)
fetch next from #spid into @spid
end
close #spid
deallocate #spid
end

原文地址:https://www.cnblogs.com/bigguai/p/2270135.html