The MERGE syntax is added to ORACLE9I: MERGE [hint] INTO [schema.] table [t_alias] USING [schema.] {table | view
MERGE syntax is added to ORACLE 9I: MERGE [hint] INTO [schema.] table [t_alias] USING [schema.] {table | view
MERGE is added to Oracle 9I.
Syntax:
MERGE [hint] INTO [schema.] table [t_alias]
USING [schema.] {table | view | subquery} [t_alias]
ON (condition)
When matched then merge_update_clause
When not matched then merge_insert_clause;
Create a test data table:
Create table tj_test (id number, name varchar2 (20), age number );
Insert data into the table:
Insert into tj_test values (1, 'Jan ', 23 );
Insert into tj_test values (2, 'kk ', 22 );
Insert into tj_test values (3, 'job', 27 );
Select * from tj_test;
The query result is as follows:
1 jan 23
2 kk 22
3 joe 27
Create another table
Create table tj_test1 as select * from tj_test where 1 = 0
Insert a data entry
Insert into tj_test1 values (1, 'jlk ', 23 );
Select * from tj_test1
The query result is as follows:
1 jkl 23 -- note that the value in the NAME field here is jkl
If MERGE is used, update is implemented, and insert is performed if no, the SQL statement is as follows:
Merge into tj_test1 tt1
Using tj_test tt
On (tt1.id = tt. id)
When matched then
Update set
Tt1.name = tt. name,
Tt1.age = tt. age
When not matched then
Insert values (
Tt. id,
Tt. name,
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)
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:
Merge into table
USING data_source
ON (condition)
When matched then update_clause
When not matched then insert_clause;
For example:
Merge into course c
USING (SELECT course_name, period,
Course_hours
FROM course_updates) cu
ON (c. course_name = cu. course_name
AND c. period = cu. period)
WHEN MATCHED THEN
UPDATE
SET c. course_hours = cu. course_hours
WHEN NOT MATCHED THEN
INSERT (c. course_name, c. period,
C. course_hours)
VALUES (cu. course_name, cu. period,
Cu. course_hours );