(@a<10), 10?, , , :
DECLARE @rows bigint
SET @rows = (SELECT COUNT(1) FROM head1)
; (@a < @rows).
Now the second problem is what you are using ID = @a, which is an invalid clause for deleted rows in your table or spaces. To solve this problem using a kind of suggestion you have to find max(ID)again and again.
I can simply suggest you use this query, but without WHILE:
DECLARE @max int
SET @max = 0
SELECT @max = CASE WHEN @max < age THEN age ELSE @max END
FROM head1
If you want to use WHILEin your path, you will need a field ROW_NUMBER()similar to this in your code:
SET @m = (SELECT h1.age
FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY head1.age) AS rn
FROM head1) h1
WHERE h1.rn = @a;
instead
select name, @m = age from head1 where ID = @a;
shA.t source
share