The Merge method in Oracle

Source: Internet
Author: User

Look at the SQL statement first: Merge into employee E using Emps em on (e.emp_id=em.emp_id)

When matched then update set E.emp_name=em.emp_name

When isn't matched then insert values (em.emp_id,em.emp_name)

1. Update the data in the Emps table to the employee table, match the primary key, execute the UPDATE statement if the primary key matches, and if the INSERT statement does not match, be aware that if the data in the Emps table matches the data of the employee table, You cannot write an INSERT statement, or you can refer to a case where a match is handled, that is, there is no when matched statement

2.using can also be used in the view or sub-query, such as the above example can also be written as:

Merge into employee e using (SELECT * from Emps) em on (e.emp_id=em.emp_id)

When matched then update set E.emp_name=em.emp_name

When isn't matched then insert values (em.emp_id,em.emp_name)

The 3.update and insert clauses can be added with a WHERE clause:

Merge into employee e using (SELECT * from Emps) em on (e.emp_id=em.emp_id)

When matched and update set e.emp_name=em.emp_name where Em.emp_name like '% of% '

When isn't matched then inserts values (em.emp_id,em.emp_name) where em.emp_name like '% sheet %'

That is, add only the data containing the sheets in the Emps table.

The 4.update clause can then delete some unwanted data with the DELETE clause, and delete can only be used with the UPDATE clause

Merge into employee e using (SELECT * from Emps) em on (e.emp_id=em.emp_id)

When matched then

Update set E.emp_name=em.emp_name delete where e.emp_id=em.emp_id where em.emp_name like '% sheet %'

When isn't matched then insert values (em.emp_id,em.emp_name)

That is, do not update the data in the Emps table that contains a single word

The Merge method in Oracle

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.