oracle 用一個表字段更新另一個表字段三種方法

1. update (select .....) set column1 = column2;

update (select iy.company_name company_name1, cc.company_name_jc company_name2
        from income_year_item iy , city_company cc
       where iy.company_code = cc.code
       )
   set company_name1 = company_name2;

 

2.只能單行子查詢s

update  CITY_PROJECT_SCALE_INFO c  set
(c.value) = (
       select d.value from CITY_PROJECT_SCALE_INFO @test d where d.project_id = '7d7fd580a06240b2a9137dc2bbe831e9'
          and d.project_id = c.project_id and c.company_code = d.company_code
)
 where exists (
  select 1 from  CITY_PROJECT_SCALE_INFO @test d where d.project_id = '7d7fd580a06240b2a9137dc2bbe831e9'
          and d.project_id = c.project_id and c.company_code = d.company_code
)

 

請你尤其注意這裏的where子句,你可以嘗試不寫where子句來執行以下這句話,你將會使得CITY_PROJECT_SCALE_INFO中的很多值變成空。

這是因爲在oracle的update語句中如果不寫where子句,oracle將會默認的把所有的值全部更新,即使你這裏使用了子查詢並且某在值並不能在子查詢裏找到,你就會想當然的以爲,oracle或許將會跳過這些值吧,你錯了,oracle將會把該行的值更新爲空。

 

3.使用merg inot 語句

--更新生產基礎字段
merge into city_cfg_data_column_common cf1 using
      city_cfg_data_column_common2 cf2 on
      (cf1.resourceid = cf2.resourceid)
   when matched then
        update set   cf1.template_type = cf2.template_type,
                     cf1.chinese_name = cf2.chinese_name,
                     cf1.column_name = cf2.column_name,
                     cf1.column_type = cf2.column_type,
                     cf1.column_size = cf2.column_size
  when not matched then
       insert (cf1.resourceid,cf1.template_type,cf1.chinese_name,cf1.column_name,cf1.column_type,cf1.column_size
              ,cf1.is_can_edit,cf1.is_unique,cf1.is_can_cover,cf1.show_order)
       values (cf2.resourceid,cf2.template_type,cf2.chinese_name,cf2.column_name,cf2.column_type,cf2.column_size
              ,cf2.is_can_edit,cf2.is_unique,cf2.is_can_cover,cf2.show_order)
              

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