Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2004-09-17 : 08:18:09
|
[code]CREATE procedure sp_murder @name varchar(128)=null, @doit bit=0 ASset nocount ondeclare @sql varchar(8000)set @sql=''IF @name IS NOT NULLselect @sql=@sql + 'KILL ' + cast(spid as varchar) + '; ' +char(13) + char(10) from sysprocesseswhere loginame LIKE '%' + @name + '%' or db_name(dbid) LIKE '%' + @name + '%' ELSEselect @sql=@sql + 'KILL ' + cast(spid as varchar) + '; ' +char(13) + char(10) from sysprocesseswhere (status='sleeping' and spid>8 AND dbid>1 AND nt_username<>'SYSTEM' and login_time<getdate()-1 and last_batch<getdate()-.25)if @doit=0 select @sql ELSE exec(@sql)[/code]Usage:exec sp_murder --lists spids that are killable cause they're oldexec sp_murder null, 1 --actually kill the spids returned from aboveexec sp_murder 'myDB', 1 --kill users in myDBexec sp_murder 'username', 1 --kill all connections made by user "username" |
|
|
|
|