Hi @HOUSSEM MAHJOUBI
Try this:
DECLARE @SampleTable TABLE (age INT,RNum INT) ;
INSERT INTO @SampleTable
SELECT age,ROW_NUMBER()OVER(ORDER BY age) AS RNum
FROM usersTable
DECLARE @Counter INT , @MaxRNum INT, @age INT
SELECT @Counter = min(RNum) , @MaxRNum = max(RNum)
FROM @SampleTable
WHILE(@Counter IS NOT NULL AND @Counter <= @MaxRNum)
BEGIN
SELECT @age = age
FROM @SampleTable WHERE RNum = @Counter
IF @age = 30
BEGIN
PRINT @age
BREAK
END
SET @Counter = @Counter + 1
END
Refer to this article for more examples: SQL WHILE loop with simple examples
Best regards,
LiHong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.