plsql编程之,循环语句和判断语句

----  作业  输出 薪水等级 2 - 5 等级 最低和 最高薪水
 set serveroutput on
declare mysal number:=1;
        myhi number;
        mylo number;
begin
  loop
    if(mysal>5) then
    exit;
    end if;
    select losal,hisal into mylo,myhi from salgrade where grade=mysal;
    dbms_output.put_line(mysal||'的最高薪水是'||myhi||'最低薪水是'||mylo);
    mysal:=mysal+1;
    end loop;
end;
--第二种方式

 set serveroutput on   --注意server的写法
declare mysal number:=2;
        myhi number;
        mylo number;
begin
  loop
    select losal,hisal into mylo,myhi from salgrade where grade=mysal;
    dbms_output.put_line(mysal||'的最高薪水是'||myhi||'最低薪水是'||mylo);
    mysal:=mysal+1;
    exit when mysal=6;
    end loop;
    
end;

--求出5的阶乘

set serveroutput on
declare a number:=5;
        b number:=1;
begin
loop
  b:=a*b;
  a:=a-1;
  exit when a=1;
end loop;
   dbms_output.put_line(b); --注意变量的作用范围、
   
end;

-- 作业  plsql  编程  查询  emp 表 薪水大于3000 
   -- 有多个  就输出  有多个员工大于3000
   -- 有1个   就输出  有1个员工大于3000
   -- 没有    就输出  没有
 set serveroutput on
 declare a number;
 begin 
   select count(*)into a from emp where sal>3000;
   if(a>1)then
   dbms_output.put_line('有一个员工大于3000');
   elsif(a=0) then   
   --else if(a=0) then                                   --注意用elsif结尾可以只用一个end if结束,否则,用多个
   dbms_output.put_line('有零个员工大于3000');
   else
   dbms_output.put_line('有多个员工大于3000');
   end if;
   --end if;
 end;  
 
 
 --case when 语句
 
 set serveroutput on
 declare a number:=1;
 
 begin
   case a             --注意此时的用法
  when 1 then
     dbms_output.put_line('优秀');
  when 2 then
     dbms_output.put_line('良好');
  else
     dbms_output.put_line('及格');
   end case;      
 end;
 
 
 --case when 语句写法2
  set serveroutput on
 declare a number:=1;
         b varchar2(32);
 
 begin
  b:= case a             --注意此时的用法
  when 1 then
    '优秀'              --没有分号
  when 2 then
    '良好'
  else
    '及格' 
   end case ; 
   dbms_output.put_line(b);  
       
 end;
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值