Hey everyone,
I am trying to update a record of a table depending on some condition. But as I am not much into sql queries, I need some help.
The problem statement is:
There are 4 columns in my table:
As you could see that InvoiceRecipientId column is having NULL.
I want to update this column (InvoiceRecipientId) and set it's value depending upon the values of AddressId and InvoiceRecipientAddressId.
Condition is that if in current record, AddressId is NOT EQUALS TO InvoiceRecipientAddressId then we have to pick the Id of the record whose AddressId is equal to the InvoiceRecipientAddressId of current record and put that Id into the InvoiceRecipientId column of current record.
Desired data in table after running the query is in below snapshot:
I used the below query in sql and it's working but not working in sql server because we can't use as with update I think.
update person as x set x.InvoiceRecipientId = (select Id from person as p where p.AddressId = x.InvoiceRecipientAddressId) where x.InvoiceRecipientAddressId = (select p.AddressId from person p where p.AddressId = InvoiceRecipientAddressId)
Please help.