PL/SQL结构化程序设计(分支结构)

一、if逻辑结构 

1.向学生表中添加记录,值为’007’ ‘Jame’ ‘计算机’ 45,并说明是否成功

declare
  v_xm varchar2(8):='jame';
  v_zym varchar2(10):='计算机';
  v_zxf number(2):=45;
begin
  insert into xs(xh,xm,zym,zxf) values('007',v_xm,v_zym,v_zxf);
  if sql%found then
    dbms_output.put_line('操作成功');
  else
    dbms_output.put_line('没有插入该人');
  end if;
end;

2.针对scott.emp表,计算7788号雇员的应交税金情况,薪金>=3000,应缴税金为薪金的0.08,薪金在1500和3000之间,应缴薪金的0.06,其它应缴0.04.

declare
  v_sal scott.emp.sal%type; //精确到列名
  v_tax scott.emp.sal%type;
begin
  select sal into v_sal from scott.emp where empno=7788;
  if v_sal>=3000   then v_tax:=v_sal*0.08;
  elsif v_sal>=1500   then v_tax:=v_sal*0.06;
  else  v_tax:=v_sal*0.04;
  end if;  
  dbms_output.put_line('应缴税金:'||v_tax);
end; 

3.涉及表为scott.emp,输入一个员工号,修改该员工的工资,如果该员工为10号部门(deptno),则要求工资增加100;若为20号部门,要求工资增加150;若为30号部门,工资增加200;否则增加300 

分析:

输入一个员工号------使用临时变量(注意:&&a为声明,&a为引用)

找出根据那个变量值的不同来做分支结构:部门号v_deptno

Select into语句求此变量,部门号v_deptno

分支结构求工资增加量

Update 语句修改工资

declare
   v_deptno scott.emp.deptno%type;
   v_zj number(4);
   v_empno scott.emp.empno%type;
 begin
   select deptno into v_deptno from scott.emp where empno=&&a2;
   if v_deptno=10  then v_zj:=100;
   elsif  v_deptno=20 then v_zj:=150;
   elsif  v_deptno=30 then v_zj:=200;
   else  v_zj:=300;
   end if;
   update scott.emp set sal=sal+v_zj where empno=&a2;
 end;

二、多分支结构case

(1)用简单型case实现

例:关于成绩等级制和百分制的相互转换。

declare
   grade varchar2(8):='良好';
 begin
   case grade
     when '优秀' then dbms_output.put_line('大于等于90');
     when '良好' then dbms_output.put_line('大于等于80,小于90');
     when '及格' then dbms_output.put_line('大于等于60,小于80');
     else dbms_output.put_line('不及格');
   end case;
 end;

扩:等值比较的case语句 

DECLARE
   v_deptno emp.deptno%type;
   v_increment NUMBER(4);
   v_empno  emp.empno%type;
   v_sal scott.emp.sal%type;
BEGIN
  v_empno:=&x;
  SELECT deptno INTO v_deptno FROM emp WHERE empno=v_empno;
 CASE v_deptno  //case 变量名
    WHEN 10 THEN v_increment:=100; //when 变量值 then 处理语句
    WHEN 20 THEN v_increment:=150;
    WHEN 30 THEN v_increment:=200;
    ELSE  v_increment:=300;
 END CASE;
  UPDATE emp SET sal=sal+v_increment WHERE empno=v_empno; 
 if SQL%FOUND then  
    dbms_output.put_line('更改成功');
    select sal into v_sal from scott.emp where empno='7788';
    dbms_output.put_line(v_sal);
 end if;
END; 

(2)用搜索型case实现

例:关于成绩等级制和百分制的相互转换。

declare
   score int:=91;
 begin
   case  //注意没有变量名
     when score>=90 then dbms_output.put_line('优秀'); //when 关系表达式 then 处理语句
     when score>=80 then dbms_output.put_line('良好');
     when score>=60 then dbms_output.put_line('及格');
     else dbms_output.put_line('不及格');
   end case;
 end;

(3)嵌入到SELECT语句执行复杂任务的CASE

用于在检索数据的同时对数据进行判断并返回判断结果

例:  通过case语句显示每一位同学的获得学分情况。

select xh,xm,zym,zxf,
 (case
     when zxf>50 then 'gao'
       when zxf>=40 then 'zhong'
         else '学分不够,需继续'
  end)
  as 获得学分情况  from xs;

效果图:

 

注意:

    1.  整个case 语句没有标点符号

     2.  case 语句的结束用end 而非end case

     3.  then 之后没有dbms_output.put_line().

     4.  as 之后无引号.

例:对于学生借阅图书项目,检验图书是否过期并显示根据过期天数确立的罚款数:

select empno,ename,job,hiredate,
(case
    when trunc(sysdate-HIREDATE)>360  then '过期'  //trunc函数是截断函数
    when hiredate is null then '没借书'
    else  '没过期'
 end) as 是否过期, 
 (case
    when trunc(sysdate-HIREDATE)>360  then (trunc(sysdate-HIREDATE))*0.1
    when hiredate is null then 0
    else  0
 end) as 是否罚款
 from scott.emp;

 (4)嵌入到PL/SQL程序语句(如赋值语句)的CASE

三、循环结构

(1)FOR循环

(2)LOOP-EXIT-END LOOP循环

(3)WHILE循环

1.LOOP-EXIT-WHEN-END循环

 求10的阶乘。

DECLARE 
         s NUMBER:=1;
	 n  NUMBER:=2;
BEGIN
    LOOP
         s:=s*n;
         n:=n+1;
	 exit when n>10;
    END LOOP;
	 dbms_output.put_line(to_char(s));
END;

 2.  WHILE-LOOP-END循环

求10的阶乘。

declare 
    s NUMBER:=1;
    n NUMBER:=2;
BEGIN
    while n<=10
       LOOP
	    s:=s*n;
	    n:=n+1;
       end LOOP;
	    dbms_output.put_line(to_char(s));
END;

3. FOR-IN-LOOP-END循环

(1)求10的阶乘。

DECLARE 
           s NUMBER:=1;
	   n NUMBER:=2;
	BEGIN
		for n in 2..10
                 Loop
		      s:=s*n;
		end loop;
		dbms_output.put_line(to_char(s));
	END;

(2)For 循环中的逆序 

DECLARE 
     s NUMBER:=1;
     n NUMBER:=2;
begin
  for n in reverse 1..10
     loop
        s:=s*n;
     end loop;
        dbms_output.put_line(to_char(s));
END;  

例:水仙花数

declare
       i int;
       a int;
       b int;
       c int;
begin  
   for i in 100..999
    loop
       a:= trunc(i/100);
       b:=trunc(i/10) mod 10;
       c:=i mod 10;
      -- dbms_output.put_line(a||' '||b||' '||c); //注释
       if (i=a*a*a+b*b*b+c*c*c) then
         dbms_output.put_line(i);
       end if;
    end loop;   
end;

※TRUNC(x,y功能: 计算截尾到y位小数的x. y缺省为0,结果变为一个整数值.

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值