1

I need to copy a column from one table to another. The problem is matching the values with the right rows.

INSERT INTO DestinationTable (money_owed)
SELECT "credit"
FROM SourceTable

How do I search through the destination table and compare another field to see if it's the same one in the source table?

Celeritas
  • 13,721
  • 33
  • 104
  • 184

2 Answers2

1

You need to join the two tables on the matching condition.

Something like this

UPDATE
   DestinationTable 
SET 
   DestinationTable.money_owed = SourceTable.Credit
FROM
   DestinationTable 
INNER JOIN SourceTable
ON DestinationTable.Field2 = SourceTable.Field2
Albin Sunnanbo
  • 45,452
  • 8
  • 67
  • 106
0

do an Update from:

UPDATE
    destination
SET
    destination.money_owed = source.Credit
FROM
    destination
INNER JOIN
    source
ON
    source.id = destination.id
Diego
  • 33,213
  • 18
  • 87
  • 131