title: In a stored procedure how can you loop table A and then update table B
body: <p>Within a stored procedure I need to loop the following table:</p>
  
  <pre><code>SELECT *
  FROM dbo.UsersInRoles
  INNER JOIN Application_Users ON UsersInRoles.AppUserID = Application_Users.AppUserID
  WHERE (UsersInRoles.ApplicationId = @ApplicationId)
  AND (UsersInRoles.RoleId = @CurrentRoleId)
  AND (Application_Users.LastLogin &lt; @StartDate)
  </code></pre>
  
  <p>And for each record that is looped I need to perform this update:</p>
  
  <pre><code>UPDATE UsersInRoles
  SET UsersInRoles.RoleId = @DenyRoleId
  WHERE (UsersInRoles.ApplicationId = @ApplicationId)
  AND (UsersInRoles.RoleId = @CurrentRoleId)
  </code></pre>
  
  <p>If there is a better way to perform this then I'm open to suggestions. Basically the 1st query does a filter based upon the <code>INNER JOIN</code> to determine which records need to be updated. Then those filtered records are looped and updated with a new <code>RoleID</code>.</p>