Loop the SQL Result


/ Published in: SQL
Save to your folder(s)

Loop the SQL Result row by row


Copy this code and paste it in your HTML
  1. DECLARE @links TABLE
  2. (idx SMALLINT PRIMARY KEY IDENTITY(1,1)
  3. , EnrolmentID INT)
  4. INSERT INTO
  5. @links
  6. (EnrolmentID)
  7. SELECT
  8. e.EnrolmentID
  9. FROM
  10. ProSolution.dbo.Enrolment e WITH (NOLOCK)
  11.  
  12. -- Start the looping for each row in Temp table
  13. DECLARE @i INT
  14. DECLARE @numrows INT
  15.  
  16. SET @i = 1
  17.  
  18. SET @numrows = (SELECT COUNT(*) FROM @links)
  19. --PRINT 'Number of Rows to Loop: ' + CONVERT(varchar(500), @numrows)
  20.  
  21. IF @numrows > 0
  22. WHILE (@i <= (SELECT MAX(idx) FROM @links))
  23. BEGIN
  24.  
  25. SELECT @EnrolmentID = EnrolmentID FROM @links WHERE idx = @i;
  26.  
  27. -- Do something now
  28.  
  29. -- INCREASE next one
  30. SET @i = @i + 1
  31. END

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.