laika222
11/2/2017 - 2:45 AM

Looping, WHILE

-- LOOPING - you don't typically loop since you're usually using set based operations, since looping can be slow and resource intensive. Loops will end when predicate evaluates to FALSE or UNKNOWN.

DECLARE @custid AS INT = 1,
@lname AS NVARCHAR(20);
WHILE @custid <=5
	BEGIN
		SELECT @lname = lastname FROM Sales.CUstomer
		WHERE customerid = @custid;
		PRINT @lname;
		SET @cusid += 1;
	END;