HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMajor

How to update a table from a another table

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
updateanotherhowfromtable

Problem

I have one table A has column (id, field_1,field_2),
and another table B has column (id,field_2)

Now I want to merge table B to A, that means i want to update field_2 in table A to value of table B. So how to implement that ? BTW I am using oracle

Thanks

Solution

An alternative approach to the co-related subquery (suggested by Kerri) would be to use the MERGE statement, which might be more efficient than the subselect (which can only be verified by looking at the execution plan of both statements).

MERGE INTO table_b 
USING 
(
  SELECT id,
         field_2
  FROM table_a
) ta ON (ta.id = table_b.id)
WHEN MATCHED THEN UPDATE 
    SET table_b.field_2 = ta.field_2

Code Snippets

MERGE INTO table_b 
USING 
(
  SELECT id,
         field_2
  FROM table_a
) ta ON (ta.id = table_b.id)
WHEN MATCHED THEN UPDATE 
    SET table_b.field_2 = ta.field_2

Context

StackExchange Database Administrators Q#3033, answer score: 30

Revisions (0)

No revisions yet.