Oracle merge into usage and Examples

Source: Internet
Author: User

The following articles mainly introduce the usage of Oracle merge into and related examples. First, let's start with adding MERGE to Oracle 9I. The following is an analysis of the specific content of the article, I hope that you will be able to provide some help in this aspect after browsing.

Syntax:

 
 
  1. MERGE [hint] INTO [schema .] table [t_alias]  
  2. USING [schema .] { table | view | subquery } [t_alias]  
  3. ON ( condition )  
  4. WHEN MATCHED THEN merge_update_clause  
  5. WHEN NOT MATCHED THEN merge_insert_clause;  
  6.  

Create a test data table:

 
 
  1. create table tj_test(id number,name varchar2(20),age number); 

Insert data into the table:

 
 
  1. insert into tj_test values (1,'jan',23);  
  2. insert into tj_test values (2,'kk',22);  
  3. insert into tj_test values (3,'joe',27);  
  4. select * from tj_test;  

The query result is as follows:

1 jan 23

2 kk 22

3 joe 27

Create another table

 
 
  1. create table tj_test1 as select * from tj_test where 1=0 

Insert a data entry

 
 
  1. insert into tj_test1 values (1,'jlk',23);  
  2. select * from tj_test1  

The query result is as follows:

1 jkl 23 -- note that the value in the NAME field here is jkl

Oracle merge into uses MERGE. If yes, it is updated. If no, It is inserted. The SQL statement is as follows:

 
 
  1. merge into tj_test1 tt1  
  2. using tj_test tt  
  3. on (tttt1.id=tt.id)  
  4. when matched then  
  5. update set  
  6. tttt1.name=tt.name,  
  7. tttt1.age=tt.age  
  8. when not matched then  
  9. insert values(  
  10. tt.id,  
  11. tt.name,  
  12. tt.age)  

Query table tj_test1 (compare the data in the original table, update the field NAME in ID = 1 ROW, and add two more data)

 
 
  1. select * from tj_test1 

Change the row data as follows:

1 jan 23 -- the original jkl value here is updated

3 joe 27 -- insert not in the original table

2 kk 22 -- insert not in the original table

Update if it exists and insert if it does not exist.

9i supports Merge, but only select subqueries are supported,

For a single data record, you can write select ...... From dual subquery.

Syntax:

 
 
  1. MERGE INTO table  
  2. USING data_source  
  3. ON (condition)  
  4. WHEN MATCHED THEN update_clause  
  5. WHEN NOT MATCHED THEN insert_clause;  
  6.  

For example:

 
 
  1. MERGE INTO course c  
  2. USING (SELECT course_name, period,  
  3. course_hours  
  4. FROM course_updates) cu  
  5. ON (c.course_name = cu.course_name  
  6. AND c.period = cu.period)  
  7. WHEN MATCHED THEN  
  8. UPDATE  
  9. SET c.course_hours = cu.course_hours  
  10. WHEN NOT MATCHED THEN  
  11. INSERT (c.course_name, c.period,  
  12. c.course_hours)  
  13. VALUES (cu.course_name, cu.period,  
  14. cu.course_hours);   

The above content is an introduction to Oracle merge into usage and examples. I hope you will get something better.

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.