title: Creating SQL UPDATE statements on the fly
body: <p>I am in the midst of updating data in multiple tables.  Currently I have a table that has one field, "sources", that is just a list of all tables that include the field "itemid".  I also have a table that has 2 fields, "itemid" and "olditemid".  In TSQL, I would like to iterate through the sources and create the update statements on the fly.  Here is what I was trying to do but I get some errors in the update statement that my variable is not declared.  I am not sure this is even close the correct way I should be doing this. Ideas?</p>
  
  <pre><code>DECLARE @tblName varchar(50)
  
  DECLARE process_cursor CURSOR FOR 
      SELECT source 
      FROM tmpTableNames
  
  OPEN process_cursor
  
  FETCH NEXT FROM processcursor 
  INTO @tblName
  
  WHILE @@FETCH_STATUS = 0
  
      UPDATE @tblName 
          SET itemid = r.itemid 
          FROM @tblName v, itemref r
          WHERE r.olditemid = v.itemid
  
      FETCH NEXT FROM process_cursor
      INTO @tblName
  
  END
  CLOSE processcursor
  DEALLOCATE processcursor
  </code></pre>