P. 1
Merge.pdf

Merge.pdf

|Views: 3|Likes:
Published by Manimala Selvam
java
java

More info:

Published by: Manimala Selvam on Nov 05, 2013
Copyright:Attribution Non-commercial
List Price: $0.99 Buy Now

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
See more
See less

11/05/2013

$0.99

USD

pdf

text

original

Oracle 10g Merge Statement

Merge Oracle’s MERGE statement is tailor-made for situations when you want to do an "upsert" i.e. update existing rows in a table or insert new rows depending on a match condition. This is typically the case when you have to synchronize a table periodically with data from another source (table/view/query). In place of 3 separate unwieldy INSERT, UPDATE and DELETE statements with conditional sub-queries, the all-in-one MERGE does the job in one shot. To illustrate how MERGE works, lets go back to the example of our STUDENT table with GMAT scores. 1 2 SQL> select * from student; 3 ID NAME SCORE 4 --------------------------------5 1 Jack 540 6 2 Rose 7 3 William 650 4 Caledon 620 8 5 Fabrizio 600 9 6 Thomas 10 7 Ruth 680 11 8 Spacer 555 12 138 rows selected. 14 Consider another table STUDENT_N, which holds updates for the target table STUDENT. Every week, table STUDENT needs to be synchronized with the data in STUDENT_N – any new entries for students who attempted the GMAT to be inserted, plus corrections if any made to the existing details. Let’s say this is how STUDENT_N looks before it is merged with STUDENT: 1SQL> select * from student_n; 2 3 ID NAME SCORE 4---------- --------------- ---------5 7 Ruth 690 8 Spicer 620 6 9 Wallace 600 7 10 Lizzy 8 11 Brock 705 9 As you can see, the following actions are required on table STUDENT: 1. 1 row for id#7 to be corrected for score: Ruth had scored 690, not 680.
1

score = b.id) 6 6 when matched then 7 update set a.score) 10 11 values (b.id = b.10. b. score 4 4 from student_n) b 5 5 on (a. name.name = b. 1 SQL> select * from student. 1 row for id#8 to be corrected for name: the student is called Spicer.id. 3 new rows (ids#9. not Spacer.name.11) to be inserted into STUDENT table.score 8 9 when not matched then 9 10 insert (a.id. MERGE statement pseudocode A bird’s eye view of the semantics of MERGE: 1 2 MERGE into <target table> USING 3 <souce table/view/result of subquery> 4 ON 5 <match condition> 6 WHEN MATCHED THEN <update clause> 7 <delete clause> 8 WHEN NOT MATCHED THEN 9 <insert clause> 10 …and finally… MERGE magic in action 1 2 SQL> merge into student a 2 using 3 3 (select id. 3.---------4 1 Jack 540 5 2 Rose 6 3 William 650 2 . a.name.score).Oracle 10g Merge Statement 2. 2 ID NAME SCORE 3 ---------.--------------. 13 Let’s check the values in table STUDENT now. b. 11 125 rows merged. a. a. 5 rows should get processed in all.name 7 8 .

Its 9i predecessor required both WHEN MATCHED and WHEN NOT MATCHED to be present in the statement. what is the point? What benefit does it give us over good old INSERT or UPDATE? Well. you cannot update a row of the target table multiple times in the same MERGE statement. MERGE might also have a performance advantage over INSERT or UPDATE – don’t assume that though. If you try updating a student’s id in the example above. Merge in 10G has improved exception reporting. Since then it has gone through a revamp to accommodate new features. You cannot update any of the columns you are merging on. 15 16 17 620 600 690 705 600 620 Sure enough. 3 . MERGE in 10G supports the delete operation as part of the update clause. 5 rows have got merged as expected – 2 updates + 3 inserts. this error will show up in 10G: ORA-38104: Columns referenced in the ON Clause cannot be updated 2. most importantly: 1. 2. do benchmark and test. You may ask why one would use MERGE for only a single operation – when there is no all-in-one "upsert" happening. MERGE is a lot more intuitive to read – and so a lot less error-prone – than the equivalent UPDATE with correlated subqueries.Oracle 10g Merge Statement 4 Caledon 7 5 Fabrizio 8 6 Thomas 9 7 Ruth 10 11 Brock 10 Lizzy 11 9 Wallace 12 8 Spicer 13 1411 rows selected. MERGE is a deterministic statement – that is. the big advantage is of ease of coding and understanding. this is no longer necessary in 10G. MERGE enhancements in Oracle 10G MERGE was introduced in Oracle 9i. 3. MERGE in 10G supports insert-only and update-only operations. Watch out for these when you MERGE 1.

score 31 9 delete where a. 1 2 ID NAME SCORE 3 -----------------------. To specify the DELETE clause.Oracle 10g Merge Statement 3. 32 335 rows merged. a.id) 29 6 when matched then update set a. 36 37 ID NAME SCORE 4 SQL> select * from student.name 30 7 8 .name = b. you must also have the DELETE privilege on the target table. 4. . o DELETE works only on rows updated during MERGE. name. score 27 3 4 from student_n) b 28 5 on (a.score < 640.---------4 1 Jack 540 5 2 Rose 3 William 650 6 4 Caledon 620 7 5 Fabrizio 600 8 6 Thomas 9 7 Ruth 690 11 Brock 705 10 10 Lizzy 11 9 Wallace 600 12 8 Spicer 620 13 1411 rows selected. 34 35SQL> select * from student. remember that: o DELETE checks the match condition on the target table. 15 16SQL> select * from student_n. 17 18 ID NAME SCORE 19---------.score = b.---------7 Ruth 690 20 8 Spicer 620 21 9 Wallace 600 22 10 Lizzy 23 11 Brock 705 24 25SQL> merge into student a 26 2 using (select id. Any rows in the target table that are not processed during MERGE are not deleted. even if they match the DELETE condition.id = b.--------------. You must have the INSERT and UPDATE privileges on the target table and the SELECT privilege on the source table. not the source. When using MERGE for the DELETE operation.

If you are using fine-grained access control on the target table. Jack. If you’re working for a customer who insists on getting individual counts. MERGE is a key-preserved operation – that is. you’d probably have to stick to the equivalent INSERT and UPDATE statements. not the target. 48 49 50 51 52 53 54 55 In the MERGE statement above. Caledon and Fabrizio’s records were not deleted from the table STUDENTS though their scores are below 640. for each source row. If the source has 3 records and all 3 are identical to the target. 5. it does not tell you the individual counts of rows inserted/updated/deleted. you’ll either have to persuade them to adapt to the new way of reporting or once again stick to the equivalent INSERT and UPDATE statements. you’re in for heartache as MERGE tends to produce large sequence gaps.--------------. 9. If this condition is not satisfied. If you are have a thing for gapless sequences. Oracle has to be able to identify a single target record for update. it is processed by the merge_update_clause if the match condition returns TRUE. The simplest method of ensuring this is to join source and target tables by the primary key of the target.---------1 Jack 540 39 2 Rose 40 3 William 650 41 4 Caledon 620 5 Fabrizio 600 42 6 Thomas 43 7 Ruth 690 44 11 Brock 705 45 10 Lizzy 46 479 rows selected. MERGE tells you the total number of rows processed in the upsert. MERGE will report ’3 rows merged’ though this merge made no difference to the target table. [Please leave a comment if you know a way out of this. error ORA-30926 will show up in 10G: o ORA-30926: unable to get a stable set of rows in the source tables 6. 5 .Oracle 10g Merge Statement 38---------. e.] 7. Oracle Database does not implement fine-grained access control during MERGE statements.g.nextval for both insert and update. The MERGE statement increments sequence. 8. since the delete condition works on the source. Even if a record in source and target is identical.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->