Created
November 17, 2022 13:53
-
-
Save stevesohcot/53511164ad7f7703d0e52860a1baaa33 to your computer and use it in GitHub Desktop.
SQL Loop Example
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
| DROP TABLE IF EXISTS #temp | |
| CREATE TABLE #temp ( | |
| id INT IDENTITY (1,1) | |
| , org VARCHAR(50) | |
| ) | |
| INSERT INTO #temp (org) | |
| SELECT 'aaa' UNION | |
| SELECT 'bbbb' UNION | |
| SELECT 'cccc' UNION | |
| SELECT 'dddd' UNION | |
| SELECT 'eeee' | |
| --SELECT * FROM #TEMP | |
| DECLARE @theOrg VARCHAR(50) = ''; | |
| DECLARE @cursorId INT = 1; | |
| DECLARE @rowCount INT = ( SELECT COUNT(*) FROM #temp); | |
| WHILE @cursorId <= @rowCount | |
| BEGIN | |
| SET @theOrg = (SELECT org FROM #temp WHERE id = @cursorId); | |
| SELECT @theOrg | |
| SET @cursorId = @cursorId + 1 | |
| END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment