Oracle控制语句(if , loop , while , for)

--if...then...elsif..then.. else...end if

declare
   num integer;
   inputno number(10);
begin
   inputno :='&请输入';
   select sal into num from scott.emp where empno=inputno;
   if(num<2000) then
     dbms_output.put_line('薪水低于2000');
   elsif (num>=2000 and num<=3000) then
     dbms_output.put_line('薪水在3000-2000之间');
   else
       dbms_output.put_line('薪水高于3000'); 
   end if;
end;


--case语句
declare
     v_deptno number:=10;
     v_sal number;
begin
 case v_deptno
      when 10 then  v_sal:=1;
      when 20 then  v_sal:=2;
      else 
           v_sal:=3;
 end case;
 update scott.emp set sal=sal+v_sal where deptno=v_deptno ;
 commit;
end;


--目标  使用case 语句统计员工薪水等级
select ename,sal,case
    when sal<2000 then '低等' 
    when sal>=2000 and sal<3000 then '中等'
    when sal>=3000 and sal<4000 then '上等'
    else  '高等'
    end  薪水等级
from emp;


--循环控制语句  loop...exit when...end loop循环控制 
declare
    v_i int:=1;
begin
    loop
        v_i:=v_i+1;
        exit when v_i=20;
        dbms_output.put_line(v_i);
    end loop; 
end;

--while...loop...end loop循环控制 
--九九乘法表
declare
  v_i number:=1;
  v_j number;
begin
  while(v_i<10)  loop
      v_j:=1;
      loop
          dbms_output.put(v_j||'*'||v_i||'='||v_j*v_i||'  ');
          v_j:=v_j+1;
          exit when v_j>v_i;
      end loop;
      dbms_output.put_line('');
      v_i:=v_i+1;
  end loop;
end;

--for循环
--for 循环变量 in [reverse] 循环下界..循环上界 loop 
       --循环处理语句段;  
--end loop;
declare 
  v_sum number:=1;
begin
   for i in  1..5 loop
       v_sum:=v_sum*i;
   end loop;
   dbms_output.put_line('阶乘结果:'||v_sum);
end;


转载自:http://blog.csdn.net/lansesl2008/article/details/13774485

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值