if語句
語法1
如果條件成立,執行if和end if 之間的語句。
if 條件表達式 then
plsql語句;
end if;
語法2
if 條件表達式 then
條件成立時執行的語句;
else
條件不成立時執行的語句;
end if;
語法3
if 條件表達式1 then
條件1成立時執行的語句;
elsif 條件表達式2 then
條件2成立時執行的語句;
...
elsif 條件表達式n then
條件n成立時執行的語句;
else
所有條件都不成立時執行的語句;
end if;
例1
```
declare
test_scores number(4,1):=&請輸入您的成績;
begin
if test_scores>=90 and test_scores<=100 then
dbms_output.put_line('您的成績是:'||test_scores||' 評測等級為:'||'A');
elsif test_scores>=80 and test_scores<90 then
dbms_output.put_line('您的成績是:'||test_scores||' 評測等級為:'||'B');
elsif test_scores>=70 and test_scores<80 then
dbms_output.put_line('您的成績是:'||test_scores||' 評測等級為:'||'C');
elsif test_scores>=60 and test_scores<70 then
dbms_output.put_line('您的成績是:'||test_scores||' 評測等級為:'||'D');
elsif test_scores<60 then
dbms_output.put_line('您的成績是:'||test_scores||' 評測等級為:'||'不合格');
else
dbms_output.put_line('請您確認成績后再試');
end if;
end;
```
例2
declare
test_scores1 number(4,1):=&請輸入您的成績;
begin
if test_scores1<60 then
dbms_output.put_line('您輸入的成績為'||test_scores1||' 經評測等級為'||'不及格');
elsif test_scores1<70 and test_scores1>=60 then
dbms_output.put_line('您輸入的成績為'||test_scores1||' 經評測等級為'||'D');
elsif test_scores1<80 and test_scores1>=70 then
dbms_output.put_line('您輸入的成績為'||test_scores1||' 經評測等級為'||'C');
elsif test_scores1<90 and test_scores1>=80 then
dbms_output.put_line('您輸入的成績為'||test_scores1||' 經評測等級為'||'B');
elsif test_scores1<=100 and test_scores1>=90 then
dbms_output.put_line('您輸入的成績為'||test_scores1||' 經評測等級為'||'A');
else
dbms_output.put_line('請您確認成績后再試');
end if;
end;
case when語句
語法1(這種語法一般只用在sql語句中,它功能和decode函數一樣)
case
when 條件表達式1 then
值1;
when 條件表達式2 then
值2;
...
when 條件表達式3 then
值2;
else
默認值
end case;
語法2
case 表達式
when 值1 then
plsql語句;
when 值2 then
plsql語句;
..
when 值3 then
plsql語句;
else
默認執行的語句;
end case;
例1
```
declare
v_scores number(4,1):=&請輸入您的成績;
v_output varchar2(50);
begin
v_output:=case
when v_scores<60 then '不及格'
when v_scores<70 and v_scores>=60 then 'D'
when v_scores<80 and v_scores>=70 then 'C'
when v_scores<90 and v_scores>=80 then 'B'
when v_scores<=100 and v_scores>=90 then 'A'
else '請您確認成績后再試' end;
dbms_output.put_line('您輸入的成績為'||v_scores||'評測等級為'||v_output);
end;
```
例2
```
declare
v_grade varchar2(20):='A';
v_score varchar2(50);
begin
v_score:=case v_grade
when '不及格' then '成績<60'
when 'D' then '60<=成績<70'
when 'C' then '70<=成績<80'
when 'B' then '80<=成績<90'
when 'A' then '90<=成績<=100'
else '請確認后再試' end;
dbms_output.put_line(v_score);
end;
```
loop
語法
loop
循環體(plsql語句);
退出循環條件;
循環變量控制語句;
end loop
循環打印1-10
```
declare
--聲明一個變量作為循環變量
n number(6):=1;
begin
--循環打印
loop
--循環體語句,打印n的值
dbms_output.put_line(n);
--退出循環條件
exit when n=10;
--循環控制語句,修改循環變量n的值
n:=n+1;
end loop;
end;
```
循環打印1-10
declare
control_var number(2):=0;
begin
loop
if control_var>9 then
exit;
end if;
control_var:=control_var+1;
dbms_output.put_line(control_var);
end loop;
end;
while
如果循環條件成立,執行循環體和循環控制語句
直到循環條件不成立退出循環
while 循環條件 loop
循環體語句;
循環控制語句;
end loop;
循環打印1-10
```
declare
--聲明一個變量作為循環變量
n number(10):=1;
begin
--while循環打印條件
while n<=10 loop
--循環體語句,打印n的值
dbms_output.put_line(n);
--循環控制語句,修改循環變量n的值
n:=n+1;
end loop;
end;
```
循環打印1-10
```
declare
n number(10):=0;
begin
while n<10 loop
n:=n+1;
dbms_output.put_line(n);
end loop;
end;
```
for循環
語法1
for 循環變量 in 數字集合 loop
循環體語句;
end loop;
數字集合的表示
1..10 表示1到10的自然數集合
循環打印1-10
```
begin
for i in 1..10 loop
dbms_output.put_line(i);
end loop;
end;
```
循環打印10-1
```
begin
for i in reverse 1..10 loop
dbms_output.put_line(i);
end loop;
end;
```
reverse表示集合順序反轉
語法2
for 循環變量 in select語句或者游標變量 loop
循環體語句;
end loop;
獲取emp表中員工編號
```
begin
for v_empno in(select empno from emp) loop
dbms_output.put_line(v_empno.empno);
end loop;
end;
```
獲取emp表中員工編號和工作
```
begin
for v_emp in(select * from emp) loop
dbms_output.put_line(v_emp.empno||','||v_emp.job);
end loop;
end;
```
獲取emp表中全部數據
```
begin
for v_emp in(select * from emp) loop
dbms_output.put_line(rpad(v_emp.empno, 4,' ')||' , '||
nvl(rpad(v_emp.ename,10,' '),' ')||' , '||
nvl(rpad(v_emp.job,9,' '),' ')||' , '||
nvl(rpad(v_emp.mgr,4,' '),' ')||' , '||
nvl(to_char(v_emp.hiredate,'YYYYMMDD'),' ')||' , '||
nvl(rpad(v_emp.sal,4,' '),' ')||' , '||
nvl(rpad(v_emp.comm,7,' '),' ')||' , '||
nvl(rpad(v_emp.deptno,2,' '),' ')
);
end loop;
end;
```
退出循環
- exit退出整個循環
- continue退出本次循環
- return直接退出程序
for
for1
```
begin
for i in 1..9 loop
if i=5 then
dbms_output.put_line('我要退出了');
exit;
end if;
dbms_output.put_line(i);
end loop;
dbms_output.put_line('小**,我在循環體外');
end;
```
執行結果
```
1
2
3
4
我要退出了
小**,我在循環體外
```
for2
```
begin
for i in 1..9 loop
if i=5 then
dbms_output.put_line('我要退出了');
continue;
end if;
dbms_output.put_line(i);
end loop;
dbms_output.put_line('小**,我在循環體外');
end;
```
執行結果
1
2
3
4
我要退出了
6
7
8
9
小**,我在循環體外
for3
```
begin
for i in 1..9 loop
if i=5 then
dbms_output.put_line('我要退出了');
return;
end if;
dbms_output.put_line(i);
end loop;
dbms_output.put_line('小**,我在循環體外');
end;
```
執行結果
1
2
3
4
我要退出了
loop
loop
```
declare
v_control number(2) := 0;
begin
loop
v_control := v_control + 1;
if v_control = 5 then
continue;
end if;
if v_control = 10 then
exit;
end if;
dbms_output.put_line(v_control);
end loop;
dbms_output.put_line('小**,我在循環體外');
end;
```
執行結果
1
2
3
4
6
7
8
9
小**,我在循環體外
while
while
```
declare
v_control number(2):=0;
begin
while v_control<99 loop
if v_control is not null then
v_control:=v_control+1;
end if;
if v_control=5 then
continue;
end if;
if v_control=10 then
return;
end if;
dbms_output.put_line(v_control);
end loop;
dbms_output.put_line('小**,我在循環體外');
end;
```
執行結果
1
2
3
4
6
7
8
9
goto(不常用)
執行到goto語句時,代碼會回到標簽所在位置,重新往下執行
標簽
```
declare
--聲明一個變量作為循環變量
n number:=1;
begin
<
> dbms_output.put_line(n); n:=n+1; if n<11 then goto lebal; end if; end; ```