oracle 迁移到 Pg 过程中,改造 merge 为 with 递归形式,但不清楚怎么改成传入 list 形式; 简化的 sql 如下
with upsert as (update xxx set state = #{state}
where code = #{code} and number = #{number} returning *)
insert into xxx select #{id}, #{state}, #{code}, #{number}
where (select count(*) from upsert) = 0;
int merge(DTO dto);
这样可以实现单条数据的 upsert,但是对传入 List<DTO>形式毫无头绪,在此求助广大 V 友~
1
yuan434356430 OP 兄弟们我自己找到解决方案了,写法是这样的~
with upsert as (update xxx set state = case when code= '1' and number = '1' then '1' when code= '2' and number = '2' then '2' end returning code, number returning *) insert into xxx select '1', '1', '1', '1' where (select count(*) from upsert where '1' = upsert.code and '1' = upsert.number) = 0 UNION ALL SELECT '2', '2', '2', '2' where (select count(*) from upsert where '2' = upsert.code and '2' = upsert.number) = 0; |
2
yuan434356430 OP 不过还是有点缺陷,就是 case when 涉及不到的条件会置空
|
3
yuan434356430 OP 加了 where 限定条件,完美
|
4
yjhatfdu2 2020-07-22 15:01:21 +08:00
pg 支持原生的 upsert,insert into tbl values(xx,xx) on conflict(some_unique_key) do update set xxx=exclude.xxx;
|
5
yuan434356430 OP @yjhatfdu2 表分区之后不支持 on conflict,哈哈哈哈哈哈哈哈哈哈哈哈
|
6
yuan434356430 OP 领导教我一种方式直接用最朴素的方式,foreach 拼 sql,然后用 pipeline 提交到数据库
|