海上月明

          editer by sun
          posts - 162, comments - 51, trackbacks - 0, articles - 8
             :: 首頁 :: 新隨筆 ::  :: 聚合  :: 管理

          from:http://www.aygfsteel.com/pts/archive/2010/11/23/338825.html

          sqlserver和oracle中實現update關聯更新的語法不同,都可以通過inline view(內嵌視圖)
          來實現,總的來說sqlserver更簡單些. 測試例子如下:


          create table tmp_a
          (cpcode varchar2(10),
          sb_ym varchar2(6),
          flag char(1)
          );

          create table tmp_b
          (cpcode varchar2(10),
          sb_ym varchar2(6),
          flag char(1)
          );

          insert into tmp_a(cpcode,sb_ym,flag)values('3201910001','200406','e');
          insert into tmp_a(cpcode,sb_ym,flag)values('3201910002','200406','e');
          insert into tmp_b(cpcode,sb_ym,flag)values('3201910001','200406','r');
          insert into tmp_b(cpcode,sb_ym,flag)values('3201910002','200406','r');
          insert into tmp_b(cpcode,sb_ym,flag)values('3201910003','200406','r');
          insert into tmp_b(cpcode,sb_ym,flag)values('3201910004','200406','e');
          commit;

          在SQLSERVER中:

          update tmp_b set flag = b.flang from tmp_a a,tmp_b b 
          where a.cpcode =b.cpcode and a.sb_ym = b.sb_ym;


          在Oracle中:

          方法一:(效率低)
          update tmp_b a
          set flag = (select flag from tmp_a b
          where a.cpcode = b.cpcode and a.sb_ym = b.sb_ym ) 
          where exists 
          (select * from tmp_a c 
          where a.cpcode = c.cpcode and a.sb_ym = c.sb_ym);

          Statistics
          ----------------------------------------------------------
          8 recursive calls
          3 db block gets
          18 consistent gets
          0 physical reads
          0 redo size

          方法二:(效率高)
          alter table tmp_a add constraint p_tmp_a primary key (cpcode, sb_ym);

          update (select b.flag flagb,a.flag flaga 
          from tmp_a a,tmp_b b 
          where a.cpcode=b.cpcode 
          and a.sb_ym=b.sb_ym) 
          set flagb=flaga;

          Statistics
          ----------------------------------------------------------
          0 recursive calls
          3 db block gets
          7 consistent gets
          0 physical reads
          0 redo size


          注意:方法二中數據源表必須要加上主鍵,否則會報錯 
          ORA-01779: 無法修改與非鍵值保存表對應的列
          被修改的表則無需增加主鍵


          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 新宾| 乌兰浩特市| 田东县| 林口县| 黑河市| 汽车| 海伦市| 张家川| 通城县| 邯郸市| 晋州市| 桓台县| 阿城市| 罗源县| 葫芦岛市| 湘潭县| 赤壁市| 封丘县| 山东省| 建平县| 曲靖市| 北辰区| 麦盖提县| 瓮安县| 巢湖市| 临洮县| 罗田县| 油尖旺区| 察雅县| 睢宁县| 崇左市| 乌拉特前旗| 神木县| 江门市| 中西区| 扬州市| 松原市| 衡东县| 平邑县| 北碚区| 平远县|