1. <var id="fe6gj"></var>

    <rp id="fe6gj"><nav id="fe6gj"></nav></rp>

    <noframes id="fe6gj"><cite id="fe6gj"></cite>

    <ins id="fe6gj"><button id="fe6gj"><p id="fe6gj"></p></button></ins>
    1. <tt id="fe6gj"><i id="fe6gj"><sub id="fe6gj"></sub></i></tt>
        始創于2000年 股票代碼:831685
        咨詢熱線:0371-60135900 注冊有禮 登錄
        • 掛牌上市企業
        • 60秒人工響應
        • 99.99%連通率
        • 7*24h人工
        • 故障100倍補償
        您的位置: 網站首頁 > 幫助中心>文章內容

        Oracle基本事務和ForAll執行批量DML練習

        發布時間:  2012/8/26 15:29:48

        基本事務的使用:
        從賬戶一的余額中轉100到賬戶二的余額中去,如果賬戶二不存在或賬戶一中的余額不足100則整筆交易回滾


        Sql代碼
        1.select * from account;   
        2.-- 創建一張賬戶表   
        3.create table account(   
        4.       -- 賬戶ID   
        5.       id number(3) not null,   
        6.       -- 賬戶名稱   
        7.       name varchar2(50) not null,   
        8.       -- 賬戶余額   
        9.       balance number(8,2) not null,   
        10.       -- 開啟時間   
        11.       btime date not null  
        12.)   
        13.-- 插入數據   
        14.insert into account(id,name,balance,btime) values(1,'張三',2000.23,to_date('2008-02-12','yyyy-mm-dd'));   
        15.insert into account(id,name,balance,btime) values(2,'李四',530,to_date('2008-10-03','yyyy-mm-dd'));   
        16.insert into account(id,name,balance,btime) values(3,'王五',1620.2,to_date('2007-08-20','yyyy-mm-dd'));   
        17.insert into account(id,name,balance,btime) values(4,'小強',910.9,to_date('2009-01-23','yyyy-mm-dd'));   
        18.insert into account(id,name,balance,btime) values(5,'小周',8700,to_date('2006-09-10','yyyy-mm-dd'));   
        19.  
        20.declare  
        21.   -- 臨時保存賬戶一的余額總數   
        22.   v_balance account.balance%type;   
        23.begin  
        24.   update account set balance = balance - 100 where name = '&轉出賬號:' returning balance into v_balance;   
        25.   if sql%notfound then  
        26.      raise_application_error(-20001,'轉出賬號 不存在!');   
        27.   end if;   
        28.   if v_balance < 0 then  
        29.      raise_application_error(-20002,'賬戶余額不足!');   
        30.   end if;   
        31.   update account set balance = balance + 100 where name = '&轉入賬號:';   
        32.   if sql%notfound then  
        33.      raise_application_error(-20003,'轉入賬號 不存在!');   
        34.   end if;   
        35.   commit;   
        36.   dbms_output.put_line('轉賬成功!');   
        37.exception   
        38.   when others then rollback;   
        39.   dbms_output.put_line(sqlerrm);   
        40.end; 
         
        1.select * from account; 
        2.-- 創建一張賬戶表   3.create table account(  4.       -- 賬戶ID   5.       id number(3) not null,  6.       -- 賬戶名稱   7.       name varchar2(50) not null,  8.       -- 賬戶余額   9.       balance number(8,2) not null,  10.       -- 開啟時間   11.       btime date not null  12.) 
        13.-- 插入數據   14.insert into account(id,name,balance,btime) values(1,'張三',2000.23,to_date('2008-02-12','yyyy-mm-dd'));  15.insert into account(id,name,balance,btime) values(2,'李四',530,to_date('2008-10-03','yyyy-mm-dd'));  16.insert into account(id,name,balance,btime) values(3,'王五',1620.2,to_date('2007-08-20','yyyy-mm-dd'));  17.insert into account(id,name,balance,btime) values(4,'小強',910.9,to_date('2009-01-23','yyyy-mm-dd'));  18.insert into account(id,name,balance,btime) values(5,'小周',8700,to_date('2006-09-10','yyyy-mm-dd'));  19. 
        20.declare  21.   -- 臨時保存賬戶一的余額總數   22.   v_balance account.balance%type; 
        23.begin  24.   update account set balance = balance - 100 where name = '&轉出賬號:' returning balance into v_balance;  25.   if sql%notfound then  26.      raise_application_error(-20001,'轉出賬號 不存在!');  27.   end if;  28.   if v_balance < 0 then  29.      raise_application_error(-20002,'賬戶余額不足!');  30.   end if;  31.   update account set balance = balance + 100 where name = '&轉入賬號:';  32.   if sql%notfound then  33.      raise_application_error(-20003,'轉入賬號 不存在!');  34.   end if;  35.   commit;  36.   dbms_output.put_line('轉賬成功!');  37.exception 
        38.   when others then rollback;  39.   dbms_output.put_line(sqlerrm); 
        40.end;  使用ForAll執行批量DML練習:
        賬戶建立超過6個月的贈送100,超過12個月的贈送200,超過24個月的贈送500,建立時間未過6個月的不贈送

        Sql代碼
        1.declare  
        2.   -- 保存建立賬戶日期與當前日期相差的份數   
        3.   v_monthbt number(5,2);   
        4.   type str_table_type is table of varchar2(50) index by binary_integer;   
        5.   type id_table_type is table of number(3) index by binary_integer;   
        6.   -- 賬戶名稱數組   
        7.   name_table str_table_type;   
        8.   -- 贈送金額數組   
        9.   money_table str_table_type;   
        10.   -- 賬戶ID數組   
        11.   id_table id_table_type;   
        12.begin  
        13.   for i in 1..5 loop   
        14.       select months_between(sysdate,btime) into v_monthbt from account where id=i;   
        15.       if v_monthbt between 6 and 12 then  
        16.          money_table(i) := 100;   
        17.       elsif v_monthbt between 12 and 24 then  
        18.          money_table(i) := 200;   
        19.       elsif v_monthbt >= 24 then  
        20.          money_table(i) := 500;   
        21.       else  
        22.          money_table(i) := 0;   
        23.       end if;   
        24.       id_table(i) := i;   
        25.   end loop;   
        26.   -- 使用ForAll批量更新數據   
        27.   forall i in 1..money_table.count  
        28.      update account set balance = balance + money_table(i) where id = id_table(i) returning name bulk collect into name_table;   
        29.      for i in 1..name_table.count loop   
        30.          dbms_output.put_line(name_table(i));   
        31.      end loop;   
        32.   commit;   
        33.end;   
        34./ 
         
        1.declare 
        2.   -- 保存建立賬戶日期與當前日期相差的份數   3.   v_monthbt number(5,2); 
        4.   type str_table_type is table of varchar2(50) index by binary_integer;  5.   type id_table_type is table of number(3) index by binary_integer;  6.   -- 賬戶名稱數組   7.   name_table str_table_type; 
        8.   -- 贈送金額數組   9.   money_table str_table_type; 
        10.   -- 賬戶ID數組   11.   id_table id_table_type; 
        12.begin  13.   for i in 1..5 loop  14.       select months_between(sysdate,btime) into v_monthbt from account where id=i;  15.       if v_monthbt between 6 and 12 then  16.          money_table(i) := 100; 
        17.       elsif v_monthbt between 12 and 24 then  18.          money_table(i) := 200; 
        19.       elsif v_monthbt >= 24 then  20.          money_table(i) := 500; 
        21.       else  22.          money_table(i) := 0; 
        23.       end if;  24.       id_table(i) := i; 
        25.   end loop;  26.   -- 使用ForAll批量更新數據   27.   forall i in 1..money_table.count  28.      update account set balance = balance + money_table(i) where id = id_table(i) returning name bulk collect into name_table;  29.      for i in 1..name_table.count loop  30.          dbms_output.put_line(name_table(i)); 
        31.      end loop;  32.   commit;  33.end;  34./ 
         


        本文出自:億恩科技【www.endtimedelusion.com】

        服務器租用/服務器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質保障!--億恩科技[ENKJ.COM]

      1. 您可能在找
      2. 億恩北京公司:
      3. 經營性ICP/ISP證:京B2-20150015
      4. 億恩鄭州公司:
      5. 經營性ICP/ISP/IDC證:豫B1.B2-20060070
      6. 億恩南昌公司:
      7. 經營性ICP/ISP證:贛B2-20080012
      8. 服務器/云主機 24小時售后服務電話:0371-60135900
      9. 虛擬主機/智能建站 24小時售后服務電話:0371-60135900
      10. 專注服務器托管17年
        掃掃關注-微信公眾號
        0371-60135900
        Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權所有  地址:鄭州市高新區翠竹街1號總部企業基地億恩大廈  法律顧問:河南亞太人律師事務所郝建鋒、杜慧月律師   京公網安備41019702002023號
          0
         
         
         
         

        0371-60135900
        7*24小時客服服務熱線

         
         
        av不卡不卡在线观看_最近2018年中文字幕_亚洲欧美一区二区三区_一级A爱做片免费观看国产_日韩在线中文天天更新_伊人中文无码在线