基本事務(wù)的使用:
從賬戶一的余額中轉(zhuǎn)100到賬戶二的余額中去,如果賬戶二不存在或賬戶一中的余額不足100則整筆交易回滾
Sql代碼
1.select * from account;
2.-- 創(chuàng)建一張賬戶表
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. -- 開啟時(shí)間
11. btime date not null
12.)
13.-- 插入數(shù)據(jù)
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,'小強(qiáng)',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. -- 臨時(shí)保存賬戶一的余額總數(shù)
22. v_balance account.balance%type;
23.begin
24. update account set balance = balance - 100 where name = '&轉(zhuǎn)出賬號(hào):' returning balance into v_balance;
25. if sql%notfound then
26. raise_application_error(-20001,'轉(zhuǎn)出賬號(hào) 不存在!');
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 = '&轉(zhuǎn)入賬號(hào):';
32. if sql%notfound then
33. raise_application_error(-20003,'轉(zhuǎn)入賬號(hào) 不存在!');
34. end if;
35. commit;
36. dbms_output.put_line('轉(zhuǎn)賬成功!');
37.exception
38. when others then rollback;
39. dbms_output.put_line(sqlerrm);
40.end;
1.select * from account;
2.-- 創(chuàng)建一張賬戶表 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. -- 開啟時(shí)間 11. btime date not null 12.)
13.-- 插入數(shù)據(jù) 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,'小強(qiáng)',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. -- 臨時(shí)保存賬戶一的余額總數(shù) 22. v_balance account.balance%type;
23.begin 24. update account set balance = balance - 100 where name = '&轉(zhuǎn)出賬號(hào):' returning balance into v_balance; 25. if sql%notfound then 26. raise_application_error(-20001,'轉(zhuǎn)出賬號(hào) 不存在!'); 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 = '&轉(zhuǎn)入賬號(hào):'; 32. if sql%notfound then 33. raise_application_error(-20003,'轉(zhuǎn)入賬號(hào) 不存在!'); 34. end if; 35. commit; 36. dbms_output.put_line('轉(zhuǎn)賬成功!'); 37.exception
38. when others then rollback; 39. dbms_output.put_line(sqlerrm);
40.end; 使用ForAll執(zhí)行批量DML練習(xí):
賬戶建立超過6個(gè)月的贈(zèng)送100,超過12個(gè)月的贈(zèng)送200,超過24個(gè)月的贈(zèng)送500,建立時(shí)間未過6個(gè)月的不贈(zèng)送
Sql代碼
1.declare
2. -- 保存建立賬戶日期與當(dāng)前日期相差的份數(shù)
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. -- 賬戶名稱數(shù)組
7. name_table str_table_type;
8. -- 贈(zèng)送金額數(shù)組
9. money_table str_table_type;
10. -- 賬戶ID數(shù)組
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批量更新數(shù)據(jù)
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. -- 保存建立賬戶日期與當(dāng)前日期相差的份數(shù) 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. -- 賬戶名稱數(shù)組 7. name_table str_table_type;
8. -- 贈(zèng)送金額數(shù)組 9. money_table str_table_type;
10. -- 賬戶ID數(shù)組 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批量更新數(shù)據(jù) 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./
本文出自:億恩科技【1tcdy.com】
服務(wù)器租用/服務(wù)器托管中國五強(qiáng)!虛擬主機(jī)域名注冊頂級(jí)提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]
|