Oracle流程控制语句

1 if...then...end if;

--流程控制语句
set SERVEROUTPUT ON
declare
var_name1 varchar2(50);
var_name2 varchar2(50);
begin
var_name1:='East';
var_name2:='xiaoke';
if length(var_name1)<length(var_name2)then
dbms_output.put_line('123');
end if;
end;

2 --if then else end if;
declare
age int:=55;
begin
if age>=56 then
dbms_output.put_line('1');
else
dbms_output.put_line('2');
end if;
end;

3 if then elsif  then ....else then ..end if;

declare
month int:=20;
begin
if month>=0 and month<=3 then
dbms_output.put_line('1');
elsif month>=4 and month<=6 then
dbms_output.put_line('2');
elsif month>=7 and month<=9 then
dbms_output.put_line('3');
elsif month>=10 and month<=12 then
dbms_output.put_line('4');
else
dbms_output.put_line('数据不合法,请检查!!!');
end if;
end;

4 case 语句

declare
season int:=3;
aboutinfo varchar2(50);
begin
case season
when 1 then aboutinfo:=season||'季度';
when 2 then aboutinfo:=season||'季度';
when 3 then aboutinfo:=season||'季度';
when 4 then aboutinfo:=season||'季度';
else
aboutinfo:=season||'季节不合法!';
end case;
dbms_output.put_line(aboutinfo);
end;

5 循环语句

--循环语句 主要包括3种循环语句 loop while for
--使用loop求前100的和 会先执行一次循环体,然后判断exit when 后面的条件表达式值是否是true或者false 如果true则跳出循环体,false继续循环
declare
sum_i int:=0;
i int:=0;
begin
loop
i:=i+1;
sum_i:=sum_i+i;
exit when i=100;
end loop;
dbms_output.put_line(sum_i);
end;

--while 语句 求得前100的和
declare
sum_i int:=100;
i int:=0;
begin
while i<=99 loop
i:=i+1;
sum_i:=sum_i+i;
end loop;
dbms_output.put_line(sum_i);
end;

--使用for 语句求得前100的和
declare
sum_i int:=0;
i int:=0;
begin
for i in reverse 1..100 loop
sum_i:=sum_i+i;
end loop;
dbms_output.put_line(sum_i);
end;

posted @ 2018-03-26 22:09  小猫钓鱼吃鱼  阅读(159)  评论(0编辑  收藏  举报