title: In SQL Server, is there a way to avoid using a Cursor?
body: <p>I have a table where each record has a Table_Name (name of a table). I then use a Cursor to select all table names related to some record in to a Cursor. Then I do a WHILE for each table name in the Cursor to do some job.</p>
  
  <p>I want to know if it's possible to solve this problem without using a Cursor.</p>
  
  <pre><code>DECLARE tables_cursor CURSOR FAST_FORWARD FOR SELECT Table_Name FROM Some_Table WHERE ...
  
  FETCH NEXT FROM tables_cursor INTO @Dynamic_Table_Name
  WHILE @@FETCH_STATUS = 0
  BEGIN
  ...
  END
  </code></pre>
  
  <p>Foreach table name in the cursor I do a dynamic SQL query like this:</p>
  
  <pre><code>SELECT @sql = '
      UPDATE dbo.' + @Dynamic_Table_Name + '
      SET ...'
  EXEC sp_executesql @sql, @params, ...
  </code></pre>
  
  <p>My question is this: Is it possible to avoid using Cursor to solve this problem?</p>
  
  <p>Unfortunately the design of having table name to reference a table can't be changed, of which I would have done immediately if I could.</p>