有则修改,无则新增 一条sql语句 效率更快版本
<insert id="insterOutMaterials">
<foreach collection="storageOutrecords" item="item" index="index" separator=";">
MERGE INTO base_storage_outrecord AS o
USING (SELECT #{item.orderno} as orderno,#{item.batch} as batch,#{item.type} as type) AS o2
ON o.orderno=o2.orderno and o.batch=o2.batch and o.type=o2.type
WHEN MATCHED
THEN UPDATE set o.num=o.num+#{item.num}
WHEN NOT MATCHED
THEN INSERT (mid,code,num,warehouse,ctime,batch,orderid,unit,orderno,type,createuserid)
VALUES
(#{item.mid},#{item.code},#{item.num},#{item.warehouse},GETDATE(),#{item.batch},(SELECT id FROM busness_product_order WHERE orderno=#{item.orderno}),#{item.unit},#{item.orderno},#{item.type},#{item.createuserid})
</foreach>
</insert>
学习永无止境!