Tutorial by Examples

The WHILE loop can be used as an alternative to CURSORS. The following example will print numbers from 0 to 99. DECLARE @i int = 0; WHILE(@i < 100) BEGIN PRINT @i; SET @i = @i+1 END
DECLARE @ID AS INT; SET @ID = (SELECT MIN(ID) from TABLE); WHILE @ID IS NOT NULL BEGIN PRINT @ID; SET @ID = (SELECT MIN(ID) FROM TABLE WHERE ID > @ID); END

Page 1 of 1