Created
July 20, 2012 12:06
-
-
Save bendras/3150378 to your computer and use it in GitHub Desktop.
SQL Server For Each Row Next
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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