始创于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.enkj.com】

服务器租用/服务器托管中国五强!虚拟主机域名注册顶级提供商!15年品质保障!--亿恩科技[ENKJ.COM]

  • 您可能在找
  • 亿恩北京公司:
  • 经营性ICP/ISP证:京B2-20150015
  • 亿恩郑州公司:
  • 经营性ICP/ISP/IDC证:豫B1.B2-20060070
  • 亿恩南昌公司:
  • 经营性ICP/ISP证:赣B2-20080012
  • 服务器/云主机 24小时售后服务电话:0371-60135900
  • 虚拟主机/智能建站 24小时售后服务电话:0371-60135900
  • 专注服务器托管17年
    扫扫关注-微信公众号
    0371-60135900
    Copyright© 1999-2019 ENKJ All Rights Reserved 亿恩科技 版权所有  地址:郑州市高新区翠竹街1号总部企业基地亿恩大厦  法律顾问:河南亚太人律师事务所郝建锋、杜慧月律师   京公网安备41019702002023号
      0
     
     
     
     

    0371-60135900
    7*24小时客服服务热线