加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-12 15:47:00 所属栏目:百科 来源:网络整理
导读:--if...then...elsif..then.. else...end ifdeclare num integer; inputno number(10);begin inputno :='请输入'; select sal into num from scott.emp where empno=inputno; if(num2000) then dbms_output.put_line('薪水低于2000'); elsif (num=2000 and n
--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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读