跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 求问各位 V 友 PG 库的批量插入问题
未分類
14 7 月 2020

求问各位 V 友 PG 库的批量插入问题

求问各位 V 友 PG 库的批量插入问题

資深大佬 : yuan434356430 22

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 友~ 

大佬有話說 (6)

  • 主 資深大佬 : yuan434356430

    兄弟们我自己找到解决方案了,写法是这样的~

    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;

  • 主 資深大佬 : yuan434356430

    不过还是有点缺陷,就是 case when 涉及不到的条件会置空

  • 主 資深大佬 : yuan434356430

    加了 where 限定条件,完美

  • 資深大佬 : yjhatfdu2

    pg 支持原生的 upsert,insert into tbl values(xx,xx) on conflict(some_unique_key) do update set xxx=exclude.xxx;

  • 主 資深大佬 : yuan434356430

    @yjhatfdu2 表分区之后不支持 on conflict,哈哈哈哈哈哈哈哈哈哈哈哈

  • 主 資深大佬 : yuan434356430

    领导教我一种方式直接用最朴素的方式,foreach 拼 sql,然后用 pipeline 提交到数据库

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具