declare @sql nvarchar(500),@temp varchar(1000);
declare @spid int;
declare @dbname varchar(100);
set @dbname='数据库名称';
set @sql='declare getspid cursor for
select spid from sysprocesses where dbid=db_id('''+@dbname+''') and spid > 50 and spid <> @@spid'
exec (@sql)
open getspid
fetch next from getspid into @spid
while @@fetch_status<>-1
begin
set @temp='kill '+rtrim(@spid)
exec(@temp)
fetch next from getspid into @spid
end
close getspid
deallocate getspid
可以复制代码,修改“数据库名称”为自己数据库的名称,就可以运行,也可以加入到存储过程或者函数中去使用。