ORACLE 兩個表結構相同,一個做為源表,一個做為補錄表,同步補錄表資料到源表的SQL語句

來源:互聯網
上載者:User
表結構
-- Create table 源表
create table INPUT_FWMJ
(
  JGBH CHAR(5) not null,
  JGMC VARCHAR2(50) not null,
  BMBH CHAR(10) not null,
  BMMC VARCHAR2(50) not null,
  FWMJ NUMBER(20,2) not null,
  ZLBZ CHAR(1) not null
)
-- Create/Recreate primary, unique and foreign key constraints
alter table INPUT_FWMJ
  add constraint 機構編號和部門編號 primary key (JGBH, BMBH)

--補錄表
-- Create table
create table INPUT_FWMJ_CHANGE
(
  TASKID     NUMBER not null,
  STATE      VARCHAR2(100),
  JGBHSOURCE CHAR(5),
  BMBHSOURCE CHAR(10),
  JGBH       CHAR(5) not null,
  JGMC       VARCHAR2(50) not null,
  BMBH       CHAR(10) not null,
  BMMC       VARCHAR2(50) not null,
  FWMJ       NUMBER(20,2) not null,
  ZLBZ       CHAR(1) not null
)

-- Create/Recreate primary, unique and foreign key constraints
alter table INPUT_FWMJ_CHANGE
  add constraint PK_INPUT_FWMJ_CHANGE primary key (JGBH, BMBH, TASKID)

              
--增加
                insert
          into input_fwmj(jgbh, jgmc, bmbh, bmmc, fwmj, zlbz)
                select jgbh, jgmc, bmbh, bmmc, fwmj, zlbz
                  from input_FWMJ_change
                 where state = 'Added'
                --刪除
                 delete input_fwmj
                 where jgbh || bmbh in
                       (select JGBHSOURCE || BMBHSOURCE
                          from input_FWMJ_change
                         where input_FWMJ_change.State = 'Deleted')
                --修改
                 update input_fwmj set
                 input_fwmj.fwmj =
                       (select input_FWMJ_change.fwmj
                          from input_FWMJ_change
                         where input_FWMJ_change.State = 'Updated'
                           and input_FWMJ_change.JGBHSOURCE =
                               input_fwmj.jgbh
                           and input_FWMJ_change.BMBHSOURCE =
                               input_fwmj.bmbh),
                 input_fwmj.zlbz =
                       (select input_FWMJ_change.zlbz
                          from input_FWMJ_change
                         where input_FWMJ_change.State = 'Updated'
                           and input_FWMJ_change.JGBHSOURCE =
                               input_fwmj.jgbh
                           and input_FWMJ_change.BMBHSOURCE =
                               input_fwmj.bmbh)
                 where exists((select 1
                                from input_FWMJ_change
                               where input_FWMJ_change.State = 'Updated'
                                 and input_FWMJ_change.JGBHSOURCE =
                                     input_fwmj.jgbh
                                 and input_FWMJ_change.BMBHSOURCE =
                                     input_fwmj.bmbh))

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.