赞
踩
MergeInto 命令是 Oracle9i 中新增的命令,有了 MergeInto 语句,我们对数据能够不仅仅只做单一的插入或单一的更新,而是可以将更新与插入一起操作。
MergeInto通过对两张表进行连接比较,如果匹配则 UPDATE,否则 INSERT
语法
MERGE INTO table_name a
USING (
(table|view|sub_query)
) b
ON (condition)
WHEN MATCHED THEN
merge_update
WHEN NOT MATCHED THEN
merge_insert
举例
MERGE INTO USER a
USING (
select * from USER
) b
ON (condition)
WHEN MATCHED THEN
merge_update
WHEN NOT MATCHED THEN
merge_insert
MERGE INTO USER a
USING (
select #{user.userid} as id from dual
) b
ON (a.USER_ID = b.id)
WHEN MATCHED THEN
UPDATE SET USER
USER_ID = b.id
USER_NAM = b.username
WHERE USER_ID = b.userid
WHEN NOT MATCHED THEN
merge_insert
正确✔:
MERGE INTO USER a
USING (
select #{user.userid} as id from dual
) b
ON (a.USER_ID = b.userid)
WHEN MATCHED THEN
UPDATE SET USER
USER_NAM = b.username
WHERE USER_ID = b.userid
WHEN NOT MATCHED THEN
merge_insert
MERGE INTO USER a
USING (
select #{user.userid} as id from dual
) b
ON (a.USER_ID = b.userid)
WHEN MATCHED THEN
UPDATE SET USER
USER_ID = b.userid
USER_NAM = b.username
WHERE USER_ID = b.userid
WHEN NOT MATCHED THEN
merge_insert
正确✔:
MERGE INTO USER a
USING (
select #{user.userid} as id from dual
) b
ON (a.USER_ID = b.userid)
WHEN MATCHED THEN
UPDATE SET USER
USER_NAM = b.username
WHERE USER_ID = b.userid
WHEN NOT MATCHED THEN
merge_insert
情况 | 结果 |
---|---|
a存在,b不存在 | nothing |
a存在,b存在 | update |
a不存在,b存在 | insert b into a |
a不存在,b不存在 | nothing |
错误❌:如果是第一次入库,b必然为null
MERGE INTO USER a
USING (
select #{user.userid} as id from USER
) b
ON (a.USER_ID = b.userid)
WHEN MATCHED THEN
UPDATE SET USER
USER_ID = b.userid
USER_NAM = b.username
WHERE USER_ID = b.userid
WHEN NOT MATCHED THEN
merge_insert
正确✔:
MERGE INTO USER a
USING (
select #{user.userid} as id from dual
) b
ON (a.USER_ID = b.userid)
WHEN MATCHED THEN
UPDATE SET USER
USER_NAM = b.username
WHERE USER_ID = b.userid
WHEN NOT MATCHED THEN
merge_insert
传统的批量入库
insert into USER(
USER_ID,USER_NAM
)
<foreach collection="list" item="item" index="index" separator="union all">
(SELECT
#{item.userid},
#{item.username}
FROM DUAL
)
</foreach>
MergeInto批量
MERGE INTO USER a USING ( <foreach collection="list" item="item" index="index" separator="union all"> (SELECT #{item.userid,jdbcType=VARCHAR} as userid, #{item.username,jdbcType=VARCHAR} as username FROM DUAL ) </foreach> ) b ON (a.USER_ID = b.userid) WHEN MATCHED THEN UPDATE SET USER USER_NAM = b.username WHERE USER_ID = b.userid WHEN NOT MATCHED THEN INSERT( USER_ID,USER_NAM )VALUES( b.userid,b.username ) )
注意
如果说我们将带有CLOB类型字段的sql按照上述语句去写,代码执行过程中会报错:java.sql.SQLSyntaxErrorException:ORA-01790:表达式必须具有对应表达式相同的数据类型。
因此,针对带有CLOB类型字段的sql有两种写法:
写法1
begin <foreach collection="list" item="item" index="index"> MERGE INTO USER a USING (SELECT #{item.userid,jdbcType=VARCHAR} as userid, #{item.username,jdbcType=CLOB} as username FROM DUAL )b ON (a.USER_ID = b.userid) WHEN MATCHED THEN UPDATE SET USER USER_NAM = b.username WHERE USER_ID = b.userid WHEN NOT MATCHED THEN INSERT( USER_ID,USER_NAM )VALUES( b.userid,b.username ); </foreach> end;
begin <foreach collection="list" item="item" index="index"> MERGE INTO USER a USING (SELECT #{item.userid,jdbcType=VARCHAR} as userid, empty_clob() as username FROM DUAL )b ON (a.USER_ID = b.userid) WHEN MATCHED THEN UPDATE SET USER USER_NAM = b.username WHERE USER_ID = b.userid WHEN NOT MATCHED THEN INSERT( USER_ID,USER_NAM )VALUES( b.userid,b.username ); <if test="item.vluResume != null"> update USER set USER_NAM = #{item.username,jdbcType=CLOB} where USER_ID = #{item.userid,jdbcType=VARCHAR} ; </if> </foreach> end;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。