oracle 如何将一个表的某个字段赋值给另一张表的某个字段

时间:2021-11-26 15:07:42

表 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