user3115933
user3115933

Reputation: 4443

Writing the SQL CASE statement to update a column based on the value of another column from another table

I need to update the values in a Column named "TravelAgencyID2" in "dbo.ReservationStay" based on the values of column "TravelAgencyTypeCode" of "dbo.TravelAgency".

The condition of the update is like this: If TravelAgencyTypeCode in dbo.TravelAgency is NOT EQUAL to 'DMC', then TravelAgencyID2 = TravelAgencyID (from dbo.ReservationStay), ELSE TravelAgencyID2 remains unchanged.

Upvotes: 0

Views: 637

Answers (1)

SWeko
SWeko

Reputation: 30882

SO, you only need to update those rows WHERE a condition is met?

UPDATE rs
SET rs.TravelAgencyID2 = rs.TravelAgencyID
FROM ReservationStay rs
  INNER JOIN TravelAgency ta on rs.TravelAgencyID2 = ta.ID
WHERE ta.TravelAgencyTypeCode  != 'DMC'

This will only update the rows you need, without touching anything else.

Upvotes: 1

Related Questions