Oracle conditional insert/update

Source: Internet
Author: User
Welcome to the Oracle community forum and interact with 2 million technical staff. Sometimes we need to update a table. When a record does not exist, we need to insert the table and update the record when it exists. We usually use if (exists (select...) update... elseinsert to scan the table twice, which is very inefficient.

Welcome to the Oracle community forum and interact with 2 million technical staff> we sometimes need to update a table and insert a record if it does not exist, update records when they exist. We usually use if (exists (select...) update... else insert to perform two-time table scan, which is very inefficient.

Welcome to the Oracle community forum and interact with 2 million technical staff> enter

We sometimes need to update a table. When a record does not exist, we need to insert the table and update the record if it exists. Normally we use if (exists (select ....) update .... else insert, which performs two-time table scanning with low efficiency. If there is a keyword merge above Oracle 9i, we can use it. Merge into requires two tag operations. We use dual

Merge into referer_stat L using (select '1' from dual) N on (L. webid = 234) when matched then update set count = count + 1 when not matched then insert (webid) values (234 );

When the referer_stat table contains the id = 234 record, the record is updated. If the referer_stat table does not exist, the record is inserted.

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.