Skip to content

Instantly share code, notes, and snippets.

@stevesohcot
Created November 17, 2022 13:53
Show Gist options
  • Select an option

  • Save stevesohcot/53511164ad7f7703d0e52860a1baaa33 to your computer and use it in GitHub Desktop.

Select an option

Save stevesohcot/53511164ad7f7703d0e52860a1baaa33 to your computer and use it in GitHub Desktop.
SQL Loop Example
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