Oracle uses merge to update or insert data (Summary)

Source: Internet
Author: User


Oracle uses merge to update or insert data (Summary) under Java code summary. Using merge is much faster than traditional first judgment and then selecting insert or update. 1) The main function is to UPDATE and insert data to the database table conditionally. If this row exists, perform an UPDATE operation. If it is a new row, execute the INSERT operation-avoid separate updates-improve performance and ease of use-the syntax of the MERGE statement is as follows: MERGE [hint] INTO [schema.] table [t_alias] USING [schema.] {table | view | subquery} [t_alias] ON (condition) when matched then merge_update_clause when not matched then merge_insert_clause; let's check the example to find out what is going ON: merge into copy_emp c USING employees e ON (c. employee_ I D = e. employee_id) when matched then update set c. first_name = e. first_name, c. last_name = e. last_name, c. department_id = e. department_id when not matched then insert values (e. employee_id, e. first_name, e. last_name, e. email, e. phone_number, e. hire_date, e. job_id, e. salary, e. commission_pct, e. manager_id, e. metn_id); merge into copy_emp c USING employees e ON (c. employee_id = e. employee_id) WHEN MATCHED THEN Update set c. first_name = e. first_name, c. last_name = e. last_name, c. department_id = e. department_id when not matched then insert values (e. employee_id, e. first_name, e. last_name, e. email, e. phone_number, e. hire_date, e. job_id, e. salary, e. commission_pct, e. manager_id, e. metn_id); 3) Use merge Note: CREATE a test TABLE: create table mm (id number, NAME VARCHAR2 (20); create table mn (id number, NAME VARCHAR2 (20); insert data IN Sert into mm values (1, 'A'); insert into mn values (1, 'B'); execute: merge into mn a using mm B ON (. ID = B. ID) when matched then update set. ID = B. id when not matched then insert values (B. ID, B. NAME); ON (. ID = B. ID) error is reported because the field used by the on clause cannot be used for update, that is, Oracle cannot update the Column Used for connection modification: merge into mn a using mm B ON (. ID = B. ID) when matched then update set. NAME = B. name when not matched then insert values (B. ID, B. NAME ); ON (. ID = B. ID) www.2cto.com INSERT: insert into mm values (1, 'C'); then execute: merge into mn a using mm B ON (. ID = B. ID) when matched then update set. NAME = B. name when not matched then insert values (B. ID, B. NAME); ON (. ID = B. an error is reported because a group of stable rows www.2cto.com 4 in the source table cannot be obtained.) update the data of the same table. Pay attention to the following details. Because the using dataset may be null, use the count () function. Merge into mn a USING (select count (*) co from mn where mn. ID = 4) B ON (B. co <> 0) -- count and <> are used here. Pay attention to why! When matched then update set a. NAME = 'E' where a. ID = 4 when not matched then insert values (4, 'E ');
 

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.