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
  • 14,489
  • 36
  • 113
  • 194

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
  • 46,430
  • 8
  • 69
  • 108
0

do an Update from:

UPDATE
    destination
SET
    destination.money_owed = source.Credit
FROM
    destination
INNER JOIN
    source
ON
    source.id = destination.id
Diego
  • 34,802
  • 21
  • 91
  • 134