您的位置:首页 > 博客中心 > 数据库 >

kill 使用当前数据库的所有session

时间:2022-03-14 02:03

 

--在维护中经常需要将某一数据库所有进程都杀掉,手工杀有点太费事。写了一个存储过程

--usage:proc_kill ‘PSADBA‘

create proc proc_kill(@db varchar(24))
as
begin
declare @str varchar(max)= ‘‘
declare @msg varchar(max)=‘‘
begin try

if db_id(@db) is null
raiserror(‘%s database not exist.‘,0,1,@db) with nowait

select @str=@str+‘kill ‘+cast(spid as varchar(4))+‘;‘ from sys.sysprocesses
where dbid=DB_ID(@db) and spid>=50
print @str
exec(@str)
end try
begin catch
set @msg = ERROR_MESSAGE()
raiserror(@msg,16,1)with nowait
end catch
end

热门排行

今日推荐

热门手游