Monday, June 30, 2008

Merge statement

Merge statement is available in Oracle since version 9i. You can use this to perform INSERT and UPDATE all in one statement.

However note that running MERGE statement in parallel can cause error situations, this happens mostly due to the fact that each parallel process tries to insert a row that doesn't exist causing primary key violations.

Here's a simple example:

Merge into d
Using (select col1, col2, col3 from src_table) s
On (d.col1 = s.col1)
When matched then
Update set d.col2 = s.col2,
d.col3 = s.col3
When not matched then
Insert (d.col2, d.col3) values (s.col2, s.col3);

No comments: