oracle中的replace?into使用及說明
oracle中的replace into
Mybaitis foreach批量insert以及配合oracle merge into函數(shù),批量update和insert
oracle中相當(dāng)于mysql的replace into函數(shù):merge into
- 因為作者使用國產(chǎn)的神通數(shù)據(jù)庫 寫法與oracle相同 沒辦法使用mysql的replace into實現(xiàn)插入
- 使用merge into來代替 不太推薦使用這個 能不用盡量不用吧
使用方法
普通使用
- xml中
<update id="mergeStudents" parameterType="com.alibaba.dto.StudentDTO"> merge into t_student a using ( SELECT #{id} as id, #{name} as name, #{age} as age FROM dual ) b on ( a.id= b.id ) when matched then UPDATE SET a.name= b.name, a.age=b.age when not matched then INSERT( a.id, a.name, a.age ) VALUES( b.id, b.name, b.age ) </update>
加上foreach
<update id="mergeStudents" parameterType="com.alibaba.dto.StudentDTO"> merge into t_student a using ( <foreach collection="stus" item="item" index="index" open="" close="" separator="union all"> SELECT #{item.id,jdbcType=VARCHAR} as id, #{item.name,jdbcType=VARCHAR} as name, #{item.age,jdbcType=VARCHAR} as age FROM dual </foreach> ) b on ( a.id= b.id ) when matched then UPDATE SET a.name= b.name, a.age=b.age when not matched then INSERT( a.id, a.name, a.age ) VALUES( b.id, b.name, b.age ) </update>
弊端:
mybatis會報一個解析不了的語法錯誤 但不影響實際結(jié)果 解決辦法暫未發(fā)現(xiàn)
com.alibaba.druid.sql.parser.ParserException: syntax error, error in :'merge into
也算mybatis的bug吧 沒有update與insert都兼容的標(biāo)簽
用oracle的merge實現(xiàn)mysql的replace into
mysql
mysql有一個replace into的dml語句,類似insert,但是會在insert之前檢查表的唯一索引或主鍵。如果存在,就改為update操作。
這在很多應(yīng)用中是一個很常用的操作。有了這個replace into ,就可以將一個 select后判斷后做update or insert改為一句話,甚是方便。
Oracle
Oracle9i引入了MERGE命令,你能夠在一個SQL語句中對一個表同時執(zhí)行inserts和upda tes操作. MERGE命令從一個或多個數(shù)據(jù)源中選擇行來updating或inserting到一個或多個表.在Oracle 10g中MERGE有如下一些改進:
1、UPDATE或INSERT子句是可選的
2、UPDATE和INSERT子句可以加WHERE子句
3、在ON條件中使用常量過濾謂詞來insert所有的行到目標(biāo)表中,不需要連接源表和目標(biāo)表
4、UPDATE子句后面可以跟DELETE子句來去除一些不需要的行
5、源表就是using關(guān)鍵字后面跟的表,目標(biāo)表就是將要被merge into的表
6、merge into 中所有的update、insert、delete都是針對目標(biāo)表來操作的。由于merge into已經(jīng)制定了操作的表,所以update、insert、delete都不需要再顯示指出表名
作用:merge into 解決用B表跟新A表數(shù)據(jù),如果A表中沒有,則把B表的數(shù)據(jù)插入A表;
語法
MERGE INTO [your table-name] [rename your table here] USING ( [write your query here] )[rename your query-sql and using just like a table] ON ([conditional expression here] AND [...]...) WHEN MATHED THEN [here you can execute some update sql or something else ] WHEN NOT MATHED THEN [execute something else here ! ]
sql demo
如下所示:
merge into qq a using (select '2022' company_no, 'cname' company_name from qq where rownum<2) b on (a.company_no = b.company_no) WHEN MATCHED THEN UPDATE SET a.company_name = a.company_name|| 'a' WHEN NOT MATCHED THEN INSERT (a.company_no, a.company_name) VALUES (b.company_no, b.company_name);
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用PLSQL遠(yuǎn)程連接Oracle數(shù)據(jù)庫的方法(內(nèi)網(wǎng)穿透)
Oracle數(shù)據(jù)庫來源于知名大廠甲骨文公司,是一款通用數(shù)據(jù)庫系統(tǒng),能提供完整的數(shù)據(jù)管理功能,而Oracle數(shù)據(jù)庫時關(guān)系數(shù)據(jù)庫的典型代表,其數(shù)據(jù)關(guān)系設(shè)計完備,這篇文章主要介紹了使用PLSQL遠(yuǎn)程連接Oracle數(shù)據(jù)庫的方法(內(nèi)網(wǎng)穿透),需要的朋友可以參考下2023-03-03Oracle數(shù)據(jù)庫常見字段類型大全以及超詳細(xì)解析
在Oracle數(shù)據(jù)庫中查詢特定表的字段個數(shù)通常需要使用SQL語句來完成,這篇文章主要介紹了Oracle數(shù)據(jù)庫常見字段類型大全以及超詳細(xì)解析,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-04-04裝Oracle用PLSQL連接登錄時不顯示數(shù)據(jù)庫的解決
這篇文章主要介紹了裝Oracle用PLSQL連接登錄時不顯示數(shù)據(jù)庫的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11