I have a database with account numbers
and card numbers
. I match these to a file to update
any card numbers to the account number, so that I am only working with account numbers.
I created a view linking the table to the account/card database to return the Table ID
and the related account number, and now I need to update those records where the ID matches with the Account Number.
This is the Sales_Import
table, where the account number
field needs to be updated:
LeadID AccountNumber
147 5807811235
150 5807811326
185 7006100100007267039
And this is the RetrieveAccountNumber
table, where I need to update from:
LeadID AccountNumber
147 7006100100007266957
150 7006100100007267039
I tried the below, but no luck so far:
UPDATE [Sales_Lead].[dbo].[Sales_Import]
SET [AccountNumber] = (SELECT RetrieveAccountNumber.AccountNumber
FROM RetrieveAccountNumber
WHERE [Sales_Lead].[dbo].[Sales_Import]. LeadID =
RetrieveAccountNumber.LeadID)
It updates the card numbers to account numbers, but the account numbers gets replaced by NULL
Generic answer for future developers.
SQL Server
Oracle (and SQL Server)
MySQL
Thanks for the responses. I found a solution tho.
I thought this is a simple example might someone get it easier,
update within the same table:
This will allow you to update a table based on the column value not being found in another table.
This will update a table based on the column value being found in both tables.
I had the same problem with
foo.new
being set tonull
for rows offoo
that had no matching key inbar
. I did something like this in Oracle: