You are missing a way to increment through your @spid_table
. You should add two lines inside your while
after the exec
DELETE FROM @spid_table WHERE spid = @count
SELECT @count = spid FROM @spid_table
or add IDENTITY column to @spid_table
and use it for loop.