Search This Blog

Friday, October 11, 2013

Loop through SQL values without a cursor.


3rd Time I had to recreate this example so I'm posting it now.


   1:   
   2:  DECLARE @Users TABLE( 
   3:    [RowNumber] Int  PRIMARY KEY,
   4:    [UserId] uniqueidentifier)
   5:   
   6:  DECLARE @iCount Int
   7:   
   8:  INSERT INTO @Users (RowNumber, UserId)
   9:  SELECT rank() Over (order by UserId)
  10:          ,UserId
  11:  FROM Profile
  12:  WHERE PropertyValuesString like '%ReportPending%'
  13:   
  14:   
  15:  SET @iCount=1
  16:  WHILE ((SELECT MAX(RowNumber) FROM @Users) >= @iCount)
  17:  BEGIN
  18:   
  19:          SELECT * FROM @Users WHERE RowNumber = @iCount
  20:          SET @iCount=@iCount+1
  21:  END

No comments: