sql – How to join multiple updates in different tables?

Question:

I have 3 updates, and the tables I have to update have 2 million more records, so it would be unfeasible to do one by one, as each one takes more than half an hour to run, and waiting for one to finish before running the other it's not productive.

How to unite them in a single update, or even in a while/for (I tried to do this, but my knowledge of SQL is limited).

Here are the updates:

update reference_bankaccount set internalid = v.internal_id 
from  contract_reference v 
where v.external_id = externalid
and internalid = ''

update reference_vendor set internalid = v.internal_id 
from  contract_reference v 
where v.external_id = externalid
and internalid = ''

update reference_customer set internalid = v.internal_id 
from  contract_reference v 
where v.external_id = externalid
and internalid = ''

I looked here on SOpt, but the guys have only been asking about changing several columns in the same table, not in different tables.

Answer:

I managed to solve it with the help of Reginaldo Rigo as follows:

update reference_bankaccount set internalid = v.internal_id 
from  contract_reference v 
where v.external_id = externalid
and internalid = '';

update reference_vendor set internalid = v.internal_id 
from  contract_reference v 
where v.external_id = externalid
and internalid = '';

update reference_customer set internalid = v.internal_id 
from  contract_reference v 
where v.external_id = externalid
and internalid = '';

Adding the ; at the end of the updates, they are executed sequentially, without me waiting to finish one to run the other. 🙂

Scroll to Top