i can see open process like this:
select sp开发者_开发技巧id from master..sysprocesses where db_name(dbid)='MyDB'
and i can kill procces like this: kill 54
but how i can kill more then one process ?
i need to kill all the open process that my select... query return to me
thank's in advance
On a busy site connections can pop up again as soon as you kill them. Do you need this?
ALTER DATABASE MyDB SET single_user WITH ROLLBACK IMMEDIATE
/*Do your task here*/
ALTER DATABASE MyDB SET multi_user
if you want to kill them all, consider using a cursor to loop over each row of of your result(s). You can probably wrap it up in a SP to make easy to use.
I typically use dynamic SQL for things like this. Much better than killing 50 processes one at a time, but obviously you want to exercise caution with what you include in the WHERE
clause and be sure you're only killing the processes you intend to, there's no undo button.
DECLARE @Sql NVARCHAR(MAX) = (
SELECT STRING_AGG(CONCAT (
'KILL '
, p.spid
), CHAR(10))
FROM sys.sysprocesses p
WHERE p.hostname = 'whatever'
)
exec(@Sql)
精彩评论