kikukawa's diary

都内で活動するシステムエンジニアが書いてます。 興味を持った技術やハマったポイント、自分用メモをつけてます。 最近はweb中心

Select文からのUpdate

SelectからUpdate

SQLServer



  UPDATE testTBL1
   SET clm1= tbl2.clm1
   FROM testTBL1 tbl1
   JOIN testTBL2 tbl2
   ON ( tbl1.Key1 = tbl2.Key1 )
   WHERE ( tbl1.Key1 Like '%' )
   AND ( tbl2.Key1 Like '%' )

Oracle

  UPDATE (
    SELECT tbl1.Key1 tbl1_Key1
      , tbl1.clm1 tbl1_clm1
      , tbl2.Key1 tbl2_Key1
      , tbl2.clm1 tbl2_clm1
     FROM testTBL1 tbl1
     JOIN testTBL2 tbl2
     ON ( tbl1.Key1 = tbl2.Key1 )
     WHERE ( tbl1.Key1 Like '%' )
     AND ( tbl2.Key1 Like '%' )
   )
   SET tbl1_clm1= tbl2_clm1
   WHERE ( tbl1_Key1 Like '%' )
   AND ( tbl2_Key1 Like '%' )

追記

update Table1 a
set(column1,column2,column3,column4) =(select b.column1,b.column2,b.column3,b.column4 from Table2 b
where b.column1=a.column2)
where exists(select 1 from Table2 b
where b.column1 = a.column1);



sqlserver

update tbl_A
set
tbl_A.CLM1=tbl_B.CLM1
,tbl_A.CLM2=tbl_B.CLM2
from tbl_B where tbl_A.KEY1 = tbl_B.KEY1 and tbl_A.KEY2 = tbl_B.KEY2


oracle
update テーブルA A
set (A.所属,A.住所,A.電話番号)
= (select B.所属,B.住所,B.電話番号 from テーブルB B where A.ID = B.ID)
where exists (select 1 from テーブルB B where A.ID = B.ID);