oracle 如何將一個表的某個字段賦值給另一張表的某個字段

表 assets_cards 的id 是表 LETTING_DETAILSES 的外鍵,表裏字段serial_number 的數據也以pre_serial_number字段存在 表LETTING_DETAILSES,id 與serial_number是一一對應的!

由於測試數據庫的表assets_cards與研發數據庫表assets_cards 中有部分資產數據的id、serial_number不一一對應,刪掉測試數據庫的不對應的數據,從研發數據庫導進這部分數據:

alter table LETTING_DETAILSES disable constraint FK51DEC93ACE7A0619;  停掉外鍵約束

1.開始導入數據ing:

insert into assets_cards s
  (id,
   accumulate_depreciation,
   address,
   amount,
   assets_group_code,
   assets_state,
   brand,
   car_number,
   category_code,
   company_code,
   cost_network_code,
   created_on,
   critical_state,
   department_code,
   discard_on,
   discard_type_code,
   duty_by,
   idle_code,
   init_original_value,
   inventory,
   invoice_number,
   lease_on,
   lessee,
   lessor,
   maintenance_cycle,
   maintenance_on,
   major_type_code,
   medium_type_code,
   month_depreciation,
   name,
   net_worth,
   opening_on,
   original_value,
   part_proprietorship_code,
   part_right_of_use_code,
   project_code,
   recorded_on,
   region_proprietorship_code,
   region_right_of_use_code,
   remarks,
   rent_money,
   rent_on,
   serial_no,
   serial_number,
   small_type_code,
   source_code,
   spec,
   state,
   state_action,
   supplier,
   type_code,
   use_by,
   use_code,
   use_network_code,
   useful_years,
   dynamic_field_template_id,
   car_full_name,
   critical_repair_state,
   repair_state_action,
   locked_receipt_id,
   idle_date,
   handle_login,
   exercise_on)

  select hibernate_sequence.nextval,
         t.accumulate_depreciation,
         t.address,
         t.amount,
         t.assets_group_code,
         t.assets_state,
         t.brand,
         t.car_number,
         t.category_code,
         t.company_code,
         t.cost_network_code,
         t.created_on,
         t.critical_state,
         t.department_code,
         t.discard_on,
         t.discard_type_code,
         t.duty_by,
         t.idle_code,
         t.init_original_value,
         t.inventory,
         t.invoice_number,
         t.lease_on,
         t.lessee,
         t.lessor,
         t.maintenance_cycle,
         t.maintenance_on,
         t.major_type_code,
         t.medium_type_code,
         t.month_depreciation,
         t.name,
         t.net_worth,
         t.opening_on,
         t.original_value,
         t.part_proprietorship_code,
         t.part_right_of_use_code,
         t.project_code,
         t.recorded_on,
         t.region_proprietorship_code,
         t.region_right_of_use_code,
         t.remarks,
         t.rent_money,
         t.rent_on,
         t.serial_no,
         t.serial_number,
         t.small_type_code,
         t.source_code,
         t.spec,
         t.state,
         t.state_action,
         t.supplier,
         t.type_code,
         t.use_by,
         t.use_code,
         t.use_network_code,
         t.useful_years,
         t.dynamic_field_template_id,
         t.car_full_name,
         t.critical_repair_state,
         t.repair_state_action,
         t.locked_receipt_id,
         t.idle_date,
         t.handle_login,
         t.exercise_on
    from assets_cards@eam0315 t
   where not exists (select serial_number
            from assets_cards s
           where t.serial_number = s.serial_number);

2.驗證是否導入重複的數據:

select serial_number from assets_cards t group by t.serial_number having count(t.serial_number)>1 ;

select id from assets_cards t group by t.id having count(t.id)>1 ;

3.確定無導入重複數據,更改表LETTING_DETAILSES的assets_card_id與表assets_cards 中的id對應

  update (select r.assets_card_id,
                 r.pre_assets_card_id,
                 r.pre_serial_number,
                 a.id aid,
                 a.serial_number
            from LETTING_DETAILSES r
            left join assets_cards a
              on r.pre_serial_number = a.serial_number
             and r.assets_card_id <> a.id) aa
     set aa.assets_card_id = aa.aid;
(有9萬條數據,用這種方法更新大概要 2秒左右)

4.重新啓用外鍵約束:

alter table LETTING_DETAILSES enable  constraint FK51DEC93ACE7A0619;

 

END

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章