Oracle updates exist, there is no insert application -merge

 

turn:

Oracle updates exist, there is no insert application -merge

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/hong10086/article/details/54342819

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

 

  1. MERGE INTO table_name alias1
  2. USING (table|view|sub_query) alias2
  3. ON (join condition)
  4. WHEN MATCHED THEN
  5. UPDATE table_name
  6. SET col1 = col_val1,
  7. col2 = col_val2
  8. WHEN NOT MATCHED THEN
  9. 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.

 

 

  1. MERGE INTO T T1
  2.  
  3. USING (SELECT '1001' AS a,2 AS b FROM dual) T2
  4.  
  5. ON ( T1.a=T2.a)
  6.  
  7. WHEN MATCHED THEN
  8.  
  9. UPDATE SET T1.b = T2.b
  10.  
  11. WHEN NOT MATCHED THEN
  12.  
  13. INSERT (a,b) VALUES(T2.a,T2.b);

 

 

 

 

Example:

mybatist in sql:

    1. <insert id= "mergeInto" >
    2.  
    3. MERGE INTO CATEGORY_EXPERT_GRADE T1
    4. USING ( select ${categoryFk} as a, ${expertFk} as b FROM dual) T2
    5. ON ( T1.CATEGORY_FK=T2.a and T1.EXPERT_FK=T2.b)
    6. WHEN MATCHED THEN
    7. UPDATE SET T1.GRADE = #{grade,jdbcType=VARCHAR}
    8. WHEN NOT MATCHED THEN
    9. insert (PK, CATEGORY_FK, EXPERT_FK,GRADE, SAVE_DATE, FLAG,EXTEND1, EXTEND2, EXTEND3)
    10. values (Sequence_Shzj.nextval, #{categoryFk,jdbcType=DECIMAL}, #{expertFk,jdbcType=DECIMAL},
    11. #{grade,jdbcType=VARCHAR}, #{saveDate,jdbcType=DATE}, #{flag,jdbcType=DECIMAL},
    12. #{extend1,jdbcType=VARCHAR}, #{extend2,jdbcType=VARCHAR}, #{extend3,jdbcType=VARCHAR}
    13. )
    14.  
    15. < /insert>

Guess you like

Origin www.cnblogs.com/libin6505/p/11528848.html