您的位置:首页 > 数据库 > Oracle

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

2016-11-03 18:21 393 查看
--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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: