title: Updating a column in table on the basis of ID and updating single rows at a time
body: <p>I have two table table1 and table2 with structure as follows . I need to update a column in table 1 with a value present in table2</p>
  
  <pre><code>Table1:
  ID , Txt1 , Txt2
  </code></pre>
  
  <p>ID is the id , Txt 1 and Txt 2 are  varchr</p>
  
  <pre><code>Table 2
  TableId, Id_in_table1 , ValueType , Value
  </code></pre>
  
  <p>TableId is the Id if the table for which data is valid . So in this case tableId would contain table id of Table1 . This ids of tables are stored in another table. We basically need to filter data on the basis of this tableId as well since we need to be sure that we are not copying data which is not valid for this table.</p>
  
  <p>Now what i need to do , update Txt1 and Txt2 of table 1 , from the table2 as per following logic,</p>
  
  <p>TableId in Table2 should match tableID of "Table 1"
  For each Id of table1 which is equal to "Id_in_table1" of table2 , 
  Put the data in table1.value into Txt1  or Txt2 depending on table2.ValueType.
  If ValueType is equals to "Type1" update table1.txt1 = table2.value
  else if ValueType is equal to "Type2" update table2.txt2 = table2.value </p>
  
  <p>There might be other valuetype possible but we dont need to do anythng in that case.</p>
  
  <p>SQL query i wrote is as follows:</p>
  
  <pre><code>update 
  table1 
  set 
  table1.txt1 = (select value from table1 c join table2 f ON c.ID = f.Id_in_table1 and f.ValueType ='Type1')
  </code></pre>
  
  <p>i was thinking to update txt1 and txt2 in two seperate queries . </p>
  
  <p>But this query will update all the rows in table1 with same value .The logic i needed was it should add to only those rows whose ID is present in table2. </p>
  
  <p>Also this query fails when the select statement returns two rows from the join, i.e if two table1.Id are present in table2.</p>
  
  <p>Can anyone help me out wiht the query for this scenarion.</p>
  
  <p>PS: i need to write write a generic query , not any db specific query.</p>
  
  <p>TIA</p>