PostgreSQL
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> PostgreSQL

Di chuyển câu lệnh Oracle MERGE sang câu lệnh PostgreSQL UPSERT

Tôi không nghĩ rằng có câu lệnh UPSERT trong PostgreSQL 9.3, nhưng bạn có thể làm điều này:

with cte_dual as (
    select
        v_c1 as key,
        v_c2 as pkey,
        v_c3 as wcount,
        v_c4 as dcount
), cte_update as (
    update my_table as a set
        wcount = b.wcount,
        dcount = b.dcount
    from cte_dual as b
    where b.key = a.key and b.pkey = a.pkey
    returning *
)
insert into my_table (key, pkey, wcount, dcount)
select d.key, d.pkey, d.wcount, d.dcount
from cte_dual as d
where not exists (select * from cte_update as u WHERE u.key = d.key and u.pkey = d.pkey)

Bạn có thể đọc một số câu hỏi tương tự:



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Cách đặt biến đường dẫn Postgres trên Windows 10

  2. LỖI:không thể tải thư viện “/opt/PostgreSQL/9.0/lib/postgresql/plperl.so”:libperl.so:

  3. Làm cách nào để di chuyển thuộc tính mô hình ActiveRecord từ json sang jsonb?

  4. GROUP BY trong mệnh đề UPDATE FROM

  5. Trình kết nối PostgreSQL trên OSGi