Assume that I have thousands of rows to update.
And I plan to do the update iteratively; by only updating 1000 rows per iteration.
And I want to iterate until there are no rows left to update.
How can I run the T-SQL script below until there is no row to update?
-- TODO: Create a loop so that it exists when there is no ROW left to be updated;
-- how can I do it?
UPDATE tableToUpdate
SET IsVegetable = 1
WHERE Id IN
(SELECT TOP 1000 Id
FROM tableToUpdate
WHERE Date = '2011-07-23 14:00')
-- Loop ends
Try this loop
Why ISNULL - because it is not clear - if the field IsVegetable is nullable or not, if not - then ISNULL not needed
When there no rows will left with IsVegetable <> 1 - the loop will quit because the @@ROWCOUNT will be = 0 or < 1000 (for the last iteration)