title: Update value in all tables that have a column name in SQL Server
body: <p>I am writing a stored proc that needs to search a database for all tables that have a certain column name.  Once I get a list of tables that have that column I need to update a value in that column.  So first I get a list of tables that have a certain column. </p>
  
  <pre><code>SELECT      c.name  AS 'ColumnName'
              ,t.name AS 'TableName'
  FROM        sys.columns c
  JOIN        sys.tables  t   ON c.object_id = t.object_id
  WHERE       c.name LIKE 'company' 
  ORDER BY    TableName
  </code></pre>
  
  <p>Now that I have a list of tables that need to be updated I need to run a query similar to the following to update the data in each table. </p>
  
  <pre><code>update table1 set company = @newValue where company = @oldvalue
  </code></pre>
  
  <p>I'm not sure how to go about writing this part.  My first thought was to write a dynamic update statement inside of a cursor like:</p>
  
  <pre><code>Declare @newValue
  Declare @oldValue
  SET @companyCursor = CURSOR FOR
  SELECT      t.name AS 'TableName'
  FROM        sys.columns c
  JOIN        sys.tables  t   ON c.object_id = t.object_id
  WHERE       c.name LIKE 'company' 
  
  OPEN @companyCursor;
  
  FETCH NEXT FROM @companyCursor INTO @tableName;
  
  WHILE @@FETCH_STATUS = 0
  BEGIN
   update @tableName set company = @newValue where company = @oldValue
   FETCH NEXT FROM @companyCursor INTO INTO @tableName;
  END
  </code></pre>
  
  <p>Is this a good strategy?</p>