Pages

Monday, October 13, 2008

SQLServerKillSession

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

Create procedure killsession(@dbname varchar(50))
as

declare @spid varchar(10)

while exists(select * from master.dbo.sysprocesses where dbid=db_id(@dbname))
begin


declare spids cursor for
select convert(varchar,spid) from master.dbo.sysprocesses
where dbid=db_id(@dbname)

open spids

while(1=1)
begin

fetch spids into @spid

if @@fetch_status < 0
break

exec('kill ' + @spid)

end
close spids
deallocate spids

end

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

0 comments: