-2

I I need to update the Subject for this whole table adding the Post Code each subject.

However, this information is on a 3rd layer table not connected directly to it. So I have to pass through 1 table to reach the information I need.

Basically is like that

Appointments.table > ClientBooking.Table > ClientInfo

I tried to link those tables but of course, I need to bring 1 result each time here is is where I lack some knowledge

UPDATE Appointments
SET Subject = Subject + (SELECT ZipCode 
                         FROM ClientInfo 
                         INNER JOIN ClientBooking ON ClientInfo.ClientID = ClientBooking.ClientID
                         INNER JOIN Appointments ON ClientBooking.AppointmentID = Appointments.UniqueID
                         WHERE Appointments.UniqueID = "EVERY ID one BY ONe")
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Possible duplicate of [SQL UPDATE SET one column to be equal to a value in a related table referenced by a different column?](https://stackoverflow.com/questions/707371/sql-update-set-one-column-to-be-equal-to-a-value-in-a-related-table-referenced-b) – Shahroozevsky Jun 10 '19 at 15:04

3 Answers3

0

Just like mentioned HERE, can you do this:

Update Appointments
set Appointments.Subject = Appointments.Subject + (Select ZipCode from ClientInfo 
                                                   INNER JOIN ClientBooking 
                                                   ON ClientInfo.ClientID = ClientBooking.ClientID
                                                   INNER JOIN Appointments AS App
                                                   ON ClientBooking.AppointmentID = App.UniqueID
                                                   where App.UniqueID = Appointments.UniqueID)
Shahroozevsky
  • 343
  • 4
  • 17
-1

Why don't you use Appointments.UniqueID IN ("comma separated id"). This will bulk update all matching rows and will avoid multiple calls.

Raka
  • 427
  • 2
  • 10
-1

Changed my code for this

SET ANSI_WARNINGS OFF
Update Appointments
set Subject = Appointments.Subject +" - "+ ClientInfo.ZipCode
FROM Appointments
Inner join ClientBooking 
ON Appointments.UniqueID = ClientBooking.AppointmentID
Inner JOIN ClientInfo
ON ClientBooking.ClientID = ClientInfo.ClientID
where Appointments.UniqueID = ClientBooking.AppointmentID
SET ANSI_WARNINGS ON

Now my code works. cheers