Merge statement in Oracle

Source: Internet
Author: User
From http://blog.chinaunix.net/space.php? Uid = 16981447 & Do = Blog & cuid = 430716 was simply formatted and a little original stuff was added. Oracle9i introduces the merge command. You can execute the inserts and updates operations on a table in an SQL statement at the same time. the merge command selects rows from one or more data sources to update or inserting to one or more tables. in Oracle 10g, merge has the following improvements:

1. The update or insert clause is optional.
2. You can add a where clause to the update and insert clauses.
3. Use the constant filter predicate in the on condition to insert all rows to the target table. You do not need to connect the source table to the target table (you can use using dual)
4. The update clause can be followed by the delete clause to remove unnecessary rows.

First, create an example table.:
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;

1. The update or insert clause that can be omitted

In Oracle 9i, the merge statement requires you to specify both the insert and update clauses. in Oracle 10 Gb, You can omit one of the update or insert clauses. the following example shows how to update the products information of the table based on whether the product_id field of the table newproducts matches:

SQL> merge into products P
2 using newproducts NP
3 On (P. product_id = NP. product_id)
4 when matched then
5 Update
6 set P. product_name = NP. product_name,
7 p. Category = NP. category;

3 rows merged.

SQL> select * from products;

Product_id product_name category
----------------------------------------
1501 vivitar 35mm sans NCS
1502 Olympus camera sans NCS
1600 play gym toys
1601 Lamaze toys
1666 Harry Potter toys
SQL>
SQL> rollback;
Rollback complete.
SQL>

In the preceding example, the merge statement affects the rows whose product IDs are 1502,160 1 and 1666. their product names and classes are updated to values in the newproducts table. in the following example, the update clause is omitted and the new product_id In the table newproducts is inserted into the table products. data that can match the product_id in the two tables is not processed. in this example, you can see that the row with product_id = 1700 is inserted into the table products.

SQL> merge into products P
2 using newproducts NP
3 On (P. product_id = NP. product_id)
4 when not matched then
5 insert
6 values (NP. product_id, NP. product_name,
7 NP. Category );

1 row merged.

SQL> select * from products;

Product_id product_name category
----------------------------------------
1501 vivitar 35mm sans NCS
1502 Olympus is50 sans NCS
1600 play gym toys
1601 Lamaze toys
1666 Harry Potter DVD
1700 wait interface books

2. Conditional updates and inserts clauses

You can add the WHERE clause to the update or insert clause to skip the update or insert operation. the following example updates the table's products data based on the table newproducts, but the field category must be matched at the same time:

SQL> merge into products P
2 using newproducts NP
3 On (P. product_id = NP. product_id)
4 when matched then
5 Update
6 set P. product_name = NP. product_name
7 where P. Category = NP. category;

2 rows merged.

SQL> select * from products;

Product_id product_name category
----------------------------------------
1501 vivitar 35mm sans NCS
1502 Olympus camera sans NCS
1600 play gym toys
1601 Lamaze toys
1666 Harry Potter DVD
SQL>
SQL> rollback;

In this example, the product IDs are 1666, 1666, and match the on condition, but the category of does not match. therefore, the merge command only updates two rows of data. the following example shows how to use the WHERE clause in both updates and inserts clauses:

SQL> merge into products P
2 using newproducts NP
3 On (P. product_id = NP. product_id)
4 when matched then
5 Update
6 set P. product_name = NP. product_name,
7 p. Category = NP. Category
8 where P. Category = 'dve'
9 when not matched then
10 insert
11 values (NP. product_id, NP. product_name, NP. Category)
12 where NP. category! = 'Books'
SQL>/

1 row merged.

SQL> select * from products;

Product_id product_name category
----------------------------------------
1501 vivitar 35mm sans NCS
1502 Olympus is50 sans NCS
1600 play gym toys
1601 Lamaze toys
1666 Harry Potter toys

SQL>

Note that because the WHERE clause insert does not insert all rows that do not match the on condition to the table products.

3. Unconditional Inserts

You can insert data from the source table to the target table without connecting the source table to the target table. this is useful when you want to insert all rows into the target table. oracle 10 Gb now supports constant filter predicates in the on condition. for example, on (1 = 0 ). the following example inserts rows from the source table to the table products without checking whether these rows exist in the table products:

SQL> merge into products P
2 using newproducts NP
3 On (1 = 0)
4 when not matched then
5 insert
6 values (NP. product_id, NP. product_name, NP. Category)
7 where NP. Category = 'books'
SQL>/

1 row merged.

SQL> select * from products;

Product_id product_name category
----------------------------------------
1501 vivitar 35mm sans NCS
1502 Olympus is50 sans NCS
1600 play gym toys
1601 Lamaze toys
1666 Harry Potter DVD
1700 wait interface books
6 rows selected.
The special table dual can be used after SQL> using. For more information, see the following example (This part is original) Procedure update_pr_data (p_rec refd_secmaster_bbuplice_tmp % rowtype) is
V_date date;
V_now date;
Begin
V_now: = sysdate;
V_date: = trunc (v_now );

Merge into refd_secmaster_bbuplice P
Using Dual
On (P. fj_secmaster_id = p_rec.fj_secmaster_id
And P. fj_price_date = v_date)
When matched then
Update
Set P. fj_modified_tms = v_now,
......
When not matched then
Insert
(Fj_secmaster_id,
...)
Values
(P_rec.fj_secmaster_id,
...
);
End;

4. Newly Added Delete clause

Merge in Oracle 10 Gb provides the option to clear rows when performing data operations. you can include the delete clause in the when matched then update clause. the delete clause must have a where condition to delete rows that match certain conditions. rows that match the Delete where condition but do not match the on condition are not deleted from the table.

In the following example, the delete clause is verified. We merge data from the table newproducts to the table products, but delete the rows whose category is into NCS.

SQL> merge into products P
2 using newproducts NP
3 On (P. product_id = NP. product_id)
4 when matched then
5 Update
6 set P. product_name = NP. product_name,
7 p. Category = NP. Category
8 Delete where (P. Category = 'your NCS ')
9 when not matched then
10 insert
11 values (NP. product_id, NP. product_name, NP. Category)
SQL>/

4 rows merged.

SQL> select * from products;

Product_id product_name category
----------------------------------------
1501 vivitar 35mm sans NCS
1600 play gym toys
1601 Lamaze toys
1666 Harry Potter toys
1700 wait interface books
SQL>

The row with the product ID 1502 is deleted from the table products because it matches both the on condition and the Delete where condition. the row with the product ID 1501 matches the Delete where condition but does not match the on condition, so it is not deleted. the row with the product ID 1700 does not match the on condition, so the table products is inserted. rows with the product ID 1601 and 1666 match the on condition but do not match the Delete where condition, so they are updated as values in the newproducts table.

Published by wiz
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.