All data in insert and update needs to be obtained from the using, so that the batch merge into effect is achieved
MERGE into Tm_user_cart_info T1
USING
(
<foreach collection= "List" item= "item" index= "index" separator= "union" >
SELECT
Case when TEMP1. F_CART_ID is NULL and then TEMP. f_cart_id ELSE TEMP1. f_cart_id END f_cart_id,
TEMP. F_goods_num,
TEMP. F_USER_ID,
TEMP. F_region_num,
TEMP. F_channel_num,
TEMP. F_goods_count
From
(SELECT #{cartid} f_cart_id,#{goodsnum} F_goods_num,#{userid} F_user_id,#{regionnum} F_region_num,#{channelnum} F_ Channel_num,#{goodscount} F_goods_count from DUAL) TEMP
Left JOIN
(
SELECT * from Tm_user_cart_info T2
WHERE 1=1 and T2. f_user_id = #{userid}
and T2. F_goods_num = #{goodsnum}
and T2. F_region_num = #{regionnum}
and T2. F_channel_num = #{channelnum}
And not EXISTS (SELECT "from Tm_user_cart_market T3 WHERE T3. f_cart_id = T2. F_CART_ID)
) TEMP1 on TEMP1. F_goods_num = TEMP. F_goods_num
</foreach>
) T
On (T1. f_cart_id = t.f_cart_id)
When matched then
UPDATE SET T1. F_goods_count = T1. F_goods_count + T.f_goods_count, T1. F_update_time = (SELECT to_char (sysdate, ' Yyyymmddhh24miss ') from DUAL)
When isn't matched then
INSERT
(f_cart_id, F_channel_num, F_merchant_num, f_user_id, F_goods_num, F_goods_count, F_add_time, F_REGION_NUM)
VALUES
(t.f_cart_id, T.f_channel_num, ", t.f_user_id, T.f_goods_num, T.f_goods_count,
(SELECT to_char (sysdate, ' Yyyymmddhh24miss ') from DUAL), T.f_region_num)
#{xxxx} changed to #{item.xxxx}
Reprinted from: http://bbs.csdn.net/topics/390940098