欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Oracle中如何使用mergeinto語句

Oracle中如何使用merge into語句,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

成都創(chuàng)新互聯(lián)公司公司2013年成立,先為城步等服務(wù)建站,城步等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為城步企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

語法介紹

像上面這樣的例子如果在SQL里面實(shí)現(xiàn)會(huì)非常簡(jiǎn)單

if exists(select 1 from T where T.a='1001' )  
   update T set T.b=2 Where T.a='1001'
else insert into T(a,b) values('1001',2);

而在Oracle里面要用到Merge into來實(shí)現(xiàn)(Oracle 9i引入的功能),其語法如下:

MERGE INTO table_name alias1 
USING (table|view|sub_query) alias2ON (join condition) 
WHEN MATCHED THEN UPDATE table_name 
    SET col1 = col_val1, 
           col2 = col_val2 
WHEN NOT MATCHED THEN INSERT (column_list) VALUES (column_values);

用法實(shí)例

我們還是直接用上一章創(chuàng)建的那個(gè)臨時(shí)表

我們先查一下數(shù)據(jù)源tskuplu

Oracle中如何使用merge into語句

可以看到我們的商品表里面有兩條數(shù)據(jù)

然后我們?cè)俨橐幌律弦徽乱呀?jīng)創(chuàng)建的臨時(shí)表temp_cstable

Oracle中如何使用merge into語句

里面什么也沒有,我們現(xiàn)在開始寫語句。

Oracle中如何使用merge into語句

判斷temp_cstable表里的incode與tskuplu表里的plucode,如果存在的話把tskuplu里面Plulong字符值更新temp_cstable里的yhtotal字段值,如果不存在的話把tskuplu里的數(shù)據(jù)插入到temp_cstable里,其中xstotal用做plulong的值默認(rèn)的yhtotal的值為0。


然后我們執(zhí)行第一遍看一下結(jié)果

Oracle中如何使用merge into語句

可以看到temp_cstable表里面有了兩條數(shù)據(jù),并且XStotal取的是tskuplu里的plulong值為1

我們?cè)傩薷囊幌抡Z句,讓剛才這個(gè)merge into的語句執(zhí)行兩次

Oracle中如何使用merge into語句

可以看到上面第一次不存在的話先插入數(shù)據(jù),如果第二次存在的話,就更新臨時(shí)表temp_cstable里面Yhtotal的值了。

完整代碼

declare vi_count integer;
    vs_sSql varchar2(4000):='';
begin
    vs_sSql:= 'select count(*) from user_tables where table_name = upper(' ||
                  chr(39) || 'temp_cstable' || chr(39) || ')';
    execute immediate vs_sSql into vi_count;
    dbms_output.put_line(vi_count);
    --判斷temp_cstable的臨時(shí)表是否存在,如果存在清空里面數(shù)據(jù),不存在即創(chuàng)建
    if vi_count>0 then
      vs_sSql := 'delete from temp_cstable';
      execute immediate vs_sSql;
    else
      vs_sSql    := '
         create global temporary table temp_cstable (
               incode varchar2(20),
               barcode varchar2(20),
               xstotal number,
               yhtotal number) on commit Preserve rows';
      execute immediate vs_sSql;
    end if;
    
    /*判斷temp_cstable表里的incode與tskuplu表里的plucode
      如果存在的話把tskuplu里面Plulong字符值更新temp_cstable里的yhtotal字段值
      如果不存在的話把tskuplu里的數(shù)據(jù)插入到temp_cstable里,其中xstotal用做plulong的值
      默認(rèn)的yhtotal的值為0
    */
    vs_sSql:= ' merge into temp_cstable t1
      using(select * from tskuplu) t2
      on (t1.incode=t2.plucode)
      when not matched then
        insert (incode,barcode,xstotal,yhtotal) values(t2.plucode,t2.pluname,t2.plulong,0)
      when matched then
        update set t1.yhtotal=t2.plulong ';
    execute immediate vs_sSql;
    
    execute immediate vs_sSql;
end;

關(guān)于Oracle中如何使用merge into語句問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

網(wǎng)站題目:Oracle中如何使用mergeinto語句
瀏覽路徑:http://chinadenli.net/article40/gpisho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)外貿(mào)建站關(guān)鍵詞優(yōu)化外貿(mào)網(wǎng)站建設(shè)網(wǎng)站內(nèi)鏈ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作