Usage of merge in Oracle

Source: Internet
Author: User

/// The merge statement is a new syntax of Oracle9i, used to merge update and insert statements. The merge statement is used to query another table based on the connection conditions of one table or subquery. The join condition matching is updated, and the insert statement cannot be executed. This syntax only requires one full table scan to complete all the work, and the execution efficiency is higher than insert + update.

 

// Table 1
Create Table subs (msid number (9 ),
Ms_type char (1 ),
Areacode number (3)
);
// Table 2
Create Table Acct (msid number (9 ),
Bill_month number (6 ),
Areacode number (3 ),
Limit number (0.00) default );

// Test Data
Insert into subs values (905310001,0, 531 );
Insert into subs values (905320001,1, 532 );
Insert into subs values (90533000533 );
Commit;
I. The following describes the basic functions of merge.
1) Use both matched and not matched clauses
Merge into Acct
Using subs B on (A. msid = B. msid)
When matched then
Update set a. areacode = B. areacode
When not matched then
Insert (msid, bill_month, areacode)
Values (B. msid, '20170101', B. areacode );
2) there is only not matched clause, that is, only insert without updating
Merge into Acct
Using subs B on (A. msid = B. msid)
When not matched then
Insert (msid, bill_month, areacode)
Values (B. msid, '20170101', B. areacode );
3) only matched clause, that is, update is not inserted.
Merge into Acct
Using subs B on (A. msid = B. msid)
When matched then
Update set a. areacode = B. areacode

Ii. 10g medium increase 1: Conditional operation
1) Use both matched and not matched clauses
Merge into Acct
Using subs B on (A. msid = B. msid)
When matched then
Update set a. areacode = B. areacode
Where B. ms_type = 0
When not matched then
Insert (msid, bill_month, areacode)
Values (B. msid, '20170101', B. areacode)
Where B. ms_type = 0;
2) there is only not matched clause, that is, only insert without updating
Merge into Acct
Using subs B on (A. msid = B. msid)
When not matched then
Insert (msid, bill_month, areacode)
Values (B. msid, '20170101', B. areacode)
Where B. ms_type = 0;
3) only matched clause, that is, update is not inserted.
Merge into Acct
Using subs B on (A. msid = B. msid)
When matched then
Update set a. areacode = B. areacode
Where B. ms_type = 0;
3. Enhancement 2 in 10 GB: delete operation
Merge into Acct
Using subs B on (A. msid = B. msid)
When matched then
Update set a. areacode = B. areacode
Delete where (B. ms_type! = 0 );

Note:
1. Update of the merge statement cannot modify the columns used for connection. Otherwise, an error is reported (the condition column after on is the join)
2. Using can be followed by (select msid, areacode from subs group by msid)
3. The most common error with merge occurs because the Acct record obtained through the connection condition is not unique.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.