1

I have two tables.

One of them TABLE1 has values like this:

TABLE1

|A   |B   |C   |D   |E   |F   |G   |H   |
-----------------------------------------
|1   |NULL|4   |NULL|NULL|NULL|NULL|10  |

TABLE2 has more values compared to TABLE1.

|A   |B   |C   |D   |E   |F   |G   |H   |
-----------------------------------------
|1   |2   |3   |NULL|5   |NULL|6   |11  |

I need to update TABLE1 with values from TABLE2 for every column with NULL values on TABLE1.

The result that I need:

|A   |B   |C   |D   |E   |F   |G   |H   |
-----------------------------------------
|1   |2   |4   |NULL|5   |NULL|6   |10  |
  • 4
    And what is the primary key in each table? – ypercubeᵀᴹ Sep 21 '16 at 21:55
  • I don't have pk for this table, because I was moving an audit table that was saving the information of the modified columns in lines (one by one) to another table copying the whole line information in just one line. – Steicy Sep 22 '16 at 16:30
  • So the tables have exactly 1 row each? – ypercubeᵀᴹ Sep 22 '16 at 17:15
  • They have after I had created a dinamic routine to put all values in the same row. But it was missing the other values. So I wanted to merge those information :) – Steicy Sep 22 '16 at 17:36
1
select isnull(t1.a, t2.a), isnull(t1.b, t2.b)  ...
from table t1 
join table t2 
  on 1 = 1 
1

I just found the answer myself using CASE WHEN for every column.

I was trying to achieve this because I was moving an audit table that was saving the information of the modified columns in lines (one by one) to another new table copying the whole line information (that could be many columns) in just one line.

Basically I was trying to merge the old data (old structure) to the new table.

  • 4
    Congrats on figuring out the solution! Any chance of your updating your answer with the whole of it (or at least as adapted to the example in your question)? It could be a nice contribution to this site's knowledge base. – Andriy M Sep 22 '16 at 6:09
  • 2
    It would be more useful to see the structure of the CASE WHEN code you used. – Paul White 9 Sep 30 '16 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.