Skip to content

Instantly share code, notes, and snippets.

@bendras
Created July 20, 2012 12:06
Show Gist options
  • Save bendras/3150378 to your computer and use it in GitHub Desktop.
Save bendras/3150378 to your computer and use it in GitHub Desktop.
SQL Server For Each Row Next
-- from: http://sqlserverplanet.com/tsql/sql-server-for-each-row-next
SELECT
RowNum = ROW_NUMBER() OVER(ORDER BY CustomerID)
,*
INTO #Customers
FROM SalesLT.Customer
DECLARE @MaxRownum int
SET @MaxRownum = (SELECT MAX(RowNum) FROM #Customers)
DECLARE @Iter int
SET @Iter = (SELECT MIN(RowNum) FROM #Customers)
WHILE @Iter <= @MaxRownum
BEGIN
SELECT *
FROM #Customers
WHERE RowNum = @Iter
-- run your operation here
SET @Iter = @Iter + 1
END
DROP TABLE #Customers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment