SQL Update one table based on another table

Here is one of the most useful SQL commands I know. The example below is a table, Sales_Import, that has incorrect AccountNumber data. So, somebody took the time to put the correct AccountNumber data into a table along with each record’s LeadID.

It is important to note that if the Sales_Import record is NOT in the RetrieveAccountNumber table based on the LeadID, the Sales_Import record has the AccountNumber field populated with null! So, be careful.

UPDATE Sales_Import B
SET AccountNumber =
(SELECT TRIM(AcctNo)
FROM RetrieveAccountNumber A
WHERE B.LeadID=A.LeadID)
WHERE B.State = ‘Michigan’

Dig deeper if you are so inspired.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s