turn:
Oracle updates exist, there is no insert application -merge
Oracle 9i introduced in the merge command,
By this merge you can simultaneously execute a SQL statement in a table inserts and updates operation course update or insert a basis for judgment on your specified condition, Merge into may be implemented by table B to update Table A data, if a table does not, put the data in table B table a is inserted. MERGE command to select rows from inserting or updating one or more data sources into one or more tables
-
MERGE INTO table_name alias1
-
USING (table|view|sub_query) alias2
-
ON (join condition)
-
WHEN MATCHED THEN
-
UPDATE table_name
-
SET col1 = col_val1,
-
col2 = col_val2
-
WHEN NOT MATCHED THEN
-
INSERT (column_list) VALUES (column_values);
In the Select alias2 out data, each related to a comparison alias1 ON (join condition), if match, the operation (the Update) is updated, if not match, the operation proceeds insert (Insert).
Therefore, strictly speaking, "at the same time there is a syntax Merge Insert and Update statements, a total number of records Insert / Update, is the number of records Using statement alias2 of."
Example:
There are T a, b two fields is a primary key. There are records (1001,2) to be saved in the T, T already exists if the update field b, is not inserted.
-
MERGE INTO T T1
-
-
USING (SELECT '1001' AS a,2 AS b FROM dual) T2
-
-
ON ( T1.a=T2.a)
-
-
WHEN MATCHED THEN
-
-
UPDATE SET T1.b = T2.b
-
-
WHEN NOT MATCHED THEN
-
-
INSERT (a,b) VALUES(T2.a,T2.b);
Example:
mybatist in sql:
-
<insert id= "mergeInto" >
-
-
MERGE INTO CATEGORY_EXPERT_GRADE T1
-
USING ( select ${categoryFk} as a, ${expertFk} as b FROM dual) T2
-
ON ( T1.CATEGORY_FK=T2.a and T1.EXPERT_FK=T2.b)
-
WHEN MATCHED THEN
-
UPDATE SET T1.GRADE = #{grade,jdbcType=VARCHAR}
-
WHEN NOT MATCHED THEN
-
insert (PK, CATEGORY_FK, EXPERT_FK,GRADE, SAVE_DATE, FLAG,EXTEND1, EXTEND2, EXTEND3)
-
values (Sequence_Shzj.nextval, #{categoryFk,jdbcType=DECIMAL}, #{expertFk,jdbcType=DECIMAL},
-
#{grade,jdbcType=VARCHAR}, #{saveDate,jdbcType=DATE}, #{flag,jdbcType=DECIMAL},
-
#{extend1,jdbcType=VARCHAR}, #{extend2,jdbcType=VARCHAR}, #{extend3,jdbcType=VARCHAR}
-
)
-
-
< /insert>