title: Loop through table rows in SQL
body: <p>I have a stored procedure with a table input. I want to somehow loop through the rows of that table. I'm not sure how you do this sort of thing in SQL.</p>
  
  <p>Here is what I am trying to do, in pseudo-SQL:</p>
  
  <pre><code>CREATE PROCEDURE RearrangePuzzles
      ChangedPuzzles table(
        OldDifficulty nvarchar(50),
        OldIndex int,
        NewDifficulty nvarchar(50),
        NewIndex int
      )
  AS
  FOREACH Row IN ChangedPuzzles 
   BEGIN
     UPDATE Puzzles
      SET Index = Row.NewIndex,
          Difficulty = Row.NewDifficulty
      WHERE Index = Row.OldIndex AND
            Difficulty = Row.OldDifficulty
   END
  </code></pre>
  
  <p>This, of course, is not valid SQL. How can I write a query with the desired functionality in SQL?</p>