Posted By

thetwai on 10/07/14


Tagged


Versions (?)

Loop the SQL Result


 / Published in: SQL
 

Loop the SQL Result row by row

  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  

You need to login to post a comment.