Gallop
Gallop

Reputation: 1405

update column based on 2 keys sql server

I have the following database tables


    Reference Table
          Code      From        To
          Abc45     200        400
          Cde78     0          128
          Fcde1     600        898

    Master Table
          Id        From        To
           1        1          100
           2        200        400
           3        0          128
           4        600        898

    Target Table
         Name      id      City      Country      Code
         West               B          CC         Fcde1
         East               V          GG         Cde78 
         North              T          TT         Abc45
         South              Z          YY         Abc45

Thanks In Advance

Upvotes: 0

Views: 118

Answers (1)

Angelo Fuchs
Angelo Fuchs

Reputation: 9941

Try this:

UPDATE  t
SET     t.id = other.id
FROM    Traget t
JOIN    (SELECT r.code, m.id
      FROM Master m
      INNER JOIN Reference r ON m.From = r.From AND m.To = r.To
      ) other
ON      t.Code = other.Code

I used this question Update with inner join? as reference.

Upvotes: 2

Related Questions