Introduction to mergeinto in Oracle

Source: Internet
Author: User
Blog. csdn. netyuzhicarticledetails1896878blog. csdn. netmacle2010articledetails5980965 this command uses a statement to update and insert data to a table from one or more data sources. in ORACLE9i, you must specify both the UPDATE and INSERT keywords when using this command.

Http://blog.csdn.net/yuzhic/article/details/1896878 http://blog.csdn.net/macle2010/article/details/5980965 the command uses a statement to UPDATE and INSERT data to a table from one or more data sources. ORACLE 9i, using this command must specify both the UPDATE and INSERT keywords, ORACLE

Http://blog.csdn.net/yuzhic/article/details/1896878

Http://blog.csdn.net/macle2010/article/details/5980965

This command uses a statement to update and insert data to a table from one or more data sources. in ORACLE 9i, the UPDATE and INSERT keywords must be specified at the same time. ORACLE 10g has made the following changes.

1. insert and update are optional. UPDATE and INSERT can be followed by the WHERE clause 3. In the ON condition, constants can be used to insert all rows to the target table, you do not need to connect to source table and target table 4. The UPDATE clause can be followed by delete to remove unnecessary rows.

Example:

Create table PRODUCTS
(
PRODUCT_ID INTEGER,
PRODUCT_NAME VARCHAR2 (60 ),
CATEGORY VARCHAR2 (60)
);

Insert into PRODUCTS values (1501, 'vivitar 123456', 'invalid NCS ');
Insert into PRODUCTS values (1502, 'olympus is50', 'using NCS ');
Insert into PRODUCTS values (1600, 'play gym', 'toys ');
Insert into PRODUCTS values (1601, 'lamaze', 'toys ');
Insert into PRODUCTS values (1666, 'Harry POTTER ', 'dve ');
Commit;


Create table newproducts

(
Product_id integer,
Product_name varchar2 (60 ),
Category varchar2 (60)
);

Insert into newproducts values (1502, 'olympus CAMERA ', 'using NCS ');
Insert into newproducts values (1601, 'lamaze', 'toys ');
Insert into newproducts values (1666, 'Harry POTTER ', 'toys ');
Insert into newproducts values (1700, 'Wait interface', 'books ');
Commit;

Update Data:

Merge into products p using newproducts np
On (p. product_id = np. product_id)
When matched then
Update
Set p. product_name = np. product_name,
P. category = np. category;

Use the product_name and category fields in the table newproducts to update the product_name and category of the same product_id In the table products.

Implementation data differential insertion:

Merge into products p
Using newproducts np
On (p. product_id = np. product_id)
When not matched then
Insert values (np. product_id, np. product_name, np. category );

When the conditions are not met, INSERT the data in the newproducts table into the products table.


Conditional insert

Merge into products p
Using newproducts np
On (1 = 1)
When not matched then
Insert
Values (np. product_id, np. product_name, np. category)
Where np. category = 'f1'

Conditional insert and update

Merge into products p
Using newproducts np
On (p. product_id = np. product_id)
When matched then
Update
Set p. product_name = np. product_name,
P. category = np. category
Where p. category = 'F3'
When not matched then
Insert
Values (np. product_id, np. product_name, np. category)
Where np. category! = 'F4'

Unconditional insert:

Merge into products p
Using newproducts np
On (1 = 0)
When not matched then
Insert
Values (np. product_id, np. product_name, np. category)
Where np. category = 'f1 ';

Delete clause;

Merge into products p
Using newproducts np
On (p. product_id = np. product_id)
When matched then
Update
Set p. product_name = np. product_name
Delete where category = 'q1 ';

The where clause can be executed to delete data in the products table only when the delete operation meets the on condition.

Before using this syntax, be sure to perform a test.

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.