海上月明

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

          日歷

          <2010年11月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          Good Blogs

          搜索

          •  

          積分與排名

          • 積分 - 262983
          • 排名 - 216

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

          sqlserver和oracle中實現(xiàn)update關(guān)聯(lián)更新的語法不同,都可以通過inline view(內(nèi)嵌視圖)
          來實現(xiàn),總的來說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


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


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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 南郑县| 邻水| 临夏县| 竹山县| 定州市| 江川县| 萍乡市| 栾川县| 临夏县| 合水县| 霸州市| 阿城市| 方山县| 公主岭市| 永嘉县| 罗定市| 万州区| 黄石市| 肇州县| 维西| 邵东县| 临朐县| 门源| 吉隆县| 锦州市| 淳化县| 宣城市| 丁青县| 姜堰市| 蓝田县| 卓资县| 城口县| 阜平县| 星子县| 剑阁县| 云阳县| 天祝| 南投县| 读书| 宝山区| 界首市|