set serverout on;
declare employee_number number;
begin
select count(*) into employee_count from employees where employee_age>30;
if employee_count>10 then
dbms_output.put_line('公司有年齡大於30的員工');
else
dbms_output.put_line('公司沒有年齡大於30的員工');
end if;
end
統計年齡大於30的員工資訊,存在多個條件判斷(elsif)時如下:
declare employee_number number;
begin
select count(*) into employee_count from employees where employee_age>30;
if employee_count=1 then
dbms_output.put_line('公司有1名年齡大於30的員工');
elsif employee_count>1 then
dbms_output.put_line('公司有多年齡大於30的員工');
else
dbms_output.put_line('公司沒有年齡大於30的員工');
end if;
end
case when 與if else 有相同的效果。當需要匹配的情況較多時,可以使流程控制更加清晰
declare employee_number number;
begin
select count(*) into employee_count from employees where employee_age>30;
case employee_count
when 1 then
dbms_output.put_line('公司有1名年齡大於30的員工');
when 0 then
dbms_output.put_line('公司沒有年齡大於30的員工');
else
dbms_output.put_line('公司有多名年齡大於30的員工');
end case
end
declare v_id number :=0;
v_name varchar2(20);
begin
loop
if v_id>=5 then
exit;--退出迴圈
end if;
v_id:=v_id+1;
select empolyee_name into v_name from employees where employee_id=v_id;
dbms_output.put_line(v_id||'號員工是'||v_name);
end loop;
end;
除了利用if判斷退出迴圈外,還可以exit when的形式跳出迴圈
while 條件判斷 loop
迴圈操作
end loop;
while指定迴圈條件,其後仍然跟loop,此處的loop迴圈與無條件迴圈中的loop完全相同,只是增加了while條件而已
declare v_id number :=0;
v_name varchar2(20);
begin
while v_id>=5
loop
v_id:=v_id+1;
select empolyee_name into v_name from employees where employee_id=v_id;
dbms_output.put_line(v_id||'號員工是'||v_name);
end loop;
end;
declare v_id number :=0;
v_name varchar2(20);
begin
for v_id in 1..5
loop
v_id:=v_id+1;
select empolyee_name into v_name from employees where employee_id=v_id;
dbms_output.put_line(v_id||'號員工是'||v_name);
end loop;
end;
1…5 表示1~5的所有整數 Oracle中的迭代(迴圈)控制語句語法
1.簡單迴圈 loop sequence of statements end loop 例 declare a number 100 begin loop a a 25 exit when a 250 end loop dbms output.put line to char a 注意這裡用to c...
oracle控制語句if else,loop等
oracle中的控制語句 1.條件語句 if then else end if if then elsif then else end if case when then when then else end case case when then when then else end case s...
Oracle 條件控制語句
pl sql有3種型別的條件控制語句 if語句 elsif語句 case語句 1.if語句 if then語句 語法格式 if condition then statement endif if then else語句 語法格式 if condition then statement1 else s...