title: How to update data in sql avoiding while loop for better performance
body: <p>I am updating data in while loop (in stored procedure) which is killing the performance badly. Is there any alternate (best) solution with relatively better performance while updating table.</p>
  
  <p>I have read about CTE but not able to transform in my current problem</p>
  
  <pre><code>            WHILE (@Counter &lt;= (SELECT COUNT(id) FROM @TopicsIds))
                   BEGIN
                     SELECT @CurrentTopicId = TopicId
                     FROM @TopicsIds
                     WHERE id = @Counter;
                     UPDATE @Modules
                    SET TopicId = @CurrentTopicId
                   WHERE id = @Counter;
  
                  SET @Counter = @Counter + 1;
                  END;
  </code></pre>