Oracle merge into usage

Source: Internet
Author: User

Purpose:
The merge command can be used to modify or insert data from one table to another. The insert or modify operation depends on the conditions of the on clause.

This statement can be executed in two steps in the same statement, which can reduce the number of insert and update statements.

Merge is a deterministic statement that does not modify the same record multiple times in the same merge statement.

Syntax:
1. into clause
Specify the target table to be modified or inserted in the into clause.

2. using clause
Specify the data source to be modified or inserted in the using clause. The data source can be a table, view, or subquery statement.

3. on Clause
In the on clause, specify the conditions to be met for executing the insert or modify operation. In each row that meets the conditions in the target table, oracle uses the corresponding data in the data source to modify these rows. For rows that do not meet the conditions, oracle inserts the corresponding data in the data source.

4. when matched | not matched
Use this clause to notify oracle how to perform operations on results that meet or do not meet the conditions. You can use the following two types of clauses.

5. merge_update clause
The merge_update clause modifies the Field Values in the target table. Executed when the on Clause condition is met. If the modification clause is executed, the modification trigger on the target table is triggered.
Restriction: a default value cannot be specified when a view is modified.

6. merge_insert clause
If the execution of the merge_insert clause does not meet the conditions of the on clause, insert data to the target table. If the insert clause is executed, the insert trigger on the target table is triggered.
. Restriction: a default value cannot be specified when a view is modified.

Merge into bonuses d

Using (select employee_id, salary, department_id from employees

Where department_id = 80) s

On (d. employee_id = s. employee_id)

When matched then update set d. bonus = d. bonus + s. salary *. 01

When not matched then insert (d. employee_id, d. bonus)

Values (s. employee_id, s. salary * 0.01 );

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.