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.
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.
SQL>
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.