I am wondering, why not use SET ROWCOUNT ?
Normally I am using rowcount because it is really easy to handle.
Set Rowcount @n
select 1
While @@ROWCOUNT > 0
BEGIN
Delete from .... where x = y
END
Should do the same without too much calculation and too many variables :hehe: