Posted on Wednesday, August 26, 2009 at 10:53 AM
First we need to setup a row counter and a row max. In this example I'm iterating through a table of coupons.
DECLARE @RowCounter int
DECLARE @RowMax int
SET @RowCounter = 1
SET @RowMax = (SELECT COUNT(*) FROM Coupon)
And this is how the while loop is created
WHILE (@RowCounter <= @RowMax)
BEGIN
-- Here you could enter real logic to process, I am just printing the RowCounter.
PRINT 'This is row ' + CAST(@RowCounter as nvarchar(10))
-- The RowCounter must be incremented each time to ensure the loop continues on.
SET @RowCounter = @RowCounter + 1
END