Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101392043
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: Oracle

2008-04-30 16:07:26

 

例一:
创建包含以下列的my_toys表.
Name   Type      
------ ------------
S_ID   VARCHAR2(5)                  
S_NAME VARCHAR2(20)                
PRICE NUMBER  
创建一个过程,过程应将每个玩具的单价增加10%,直到所有玩具的平均价格达到400.此外,过程还应保证任一玩具的价格不超过500.

--创建包
--规范
create or replace package pack1 is
procedure updatetoyprice;
end pack1;
--主体
create or replace package body pack1 is
procedure updatetoyprice is
avg_pri number;
begin
  select avg(price) into avg_pri from my_toys;
  while avg_pri<400
  loop
      update my_toys set price=price/0.1 where price<500;
      select avg(price) into avg_pri from my_toys;--需重新找出平均值,因为每循环一次,值都在变化.不要次句为死循环!!
  end loop;
end updatetoyprice;
end pack1;
--调用
declare
begin
  pack1.updatetoyprice;
end;
select *From my_toys;

例二(oracle中的case用法):
编写一个计算员工应缴个人所得税的函数,当工资低于800元时,个人所得税为0,
当工资低于800至1499元时,个人所得税为本人工资的5%;当工资在1500至1999元时,
个人所得税为本人工资的10%;当工资在2000至2999元时,个人所得税为本人工资的15%;
当工资高于3000元时,个人所得税为本人工资的20%。
create or replace function fun_sal(eno number) return number
is
  s_sal number;
  r_sal number;
begin
  select sal into s_sal from emp where empno=eno;
  case
      when s_sal<800 then r_sal:=s_sal;
      when s_sal>800 and s_sal<1499 then r_sal:=s_sal*0.5;
      when s_sal>1500 and s_sal<1999 then r_sal:=s_sal*0.1;
      when s_sal>2000 and s_sal<2999 then r_sal:=s_sal*0.15;
      else r_sal:=s_sal*0.2;
  end case;
  return r_sal;
end fun_sal;
--调用
declare
sno number;
r_sal number;
begin
  sno:=&请输入编号;
  r_sal:=fun_sal(sno);
  dbms_output.put_line('个人所得税为:'||r_sal);
end;

 

原文:http://hujing1229.blog.ccidnet.com/blog-htm-itemid-187894-do-showone-type-blog-uid-60604.html


阅读(299) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~