if-then 语法:
if 条件 thenendif;--例:declare
a number(2):=10;beginif(a<20)then
dbms_output.put_line('a 小于 20 ');endif;
dbms_output.put_line('a 的值 '|| a);end;
2. if - then - else语句
if-then-else 语句的语法:
if 条件 then
S1;else
S2;endif;--例:declare number(2) :=20;beginif( a <20)then
dbms_output.put_line('a 小于 20 ');else
dbms_output.put_line('a 不小于 20 ');endif;
dbms_output.put_line('a 的值 '|| a);end;
3. if - then - elsif语句
/*
if - then - elsif语句
1.判断成年还是未成年(18岁)(单分支结构)
2.青年 (18-40)中年老年 (多分支结构)
if 多分支
*/declare
a int :=&age;beginif a <18then
dbms_output.put_line('未成年!');
elsif a between18and45then
dbms_output.put_line('青年!');
elsif a between'46'and'69'then
dbms_output.put_line('中年!');
elsif a between'18'and'45'then
dbms_output.put_line('老年!');else
dbms_output.put_line('年龄不符合要求!');endif;end;
4. case语句
declare
a int :=&a;--接受键盘输入数据begincasewhen a <18then
dbms_output.put_line('未成年!');when18< a and a <45then
dbms_output.put_line('青年!');when46< a and a <69then
dbms_output.put_line('中年!');when a >69and a <100then
dbms_output.put_line('老年!');else
dbms_output.put_line('年龄不符合要求!');endcase;end;
循环语句
1. 基本loop循环
语法:
LOOP
Sequence of statements;ENDLOOP;
例子:
declare
x number:=10;beginloop
dbms_output.put_line(x);
x := x+10;--第一种退出方式/*if x > 50 then
exit;
end if;*/--第二种退出方式exitwhen x>50;endloop;
dbms_output.put_line('循环结束后x的值是'||x);end;
2. while loop循环
基本语法:
WHILE condition LOOP
sequence_of_statements
ENDLOOP;
例子:
DECLARE x number :=10;BEGINWHILE x <=50LOOP
dbms_output.put_line('a 的值是 '|| x);
x := x +10;ENDLOOP;
dbms_output.put_line('循环结束后,x 的值是: '|| x);END;
3. for loop循环
FORLOOP 语法:
FOR counter IN initial_value .. final_value LOOP
sequence_of_statements;ENDLOOP;
例子:
DECLARE a number(2);BEGINFOR a in10..20LOOP
dbms_output.put_line('a 的值是'|| a);ENDLOOP;END;
4. 嵌套循环
(1)PL/SQL 中嵌套的基本 LOOP 语句的语法如下
LOOP
Sequence of statements1
LOOP Sequence of statements2
ENDLOOP;ENDLOOP;
(2)PL/SQL 中嵌套 FORLOOP 语句的语法:
FOR counter1 IN initial_value1 .. final_value1 LOOP
sequence_of_statements1
FOR counter2 IN initial_value2 .. final_value2 LOOP
sequence_of_statements2
ENDLOOP;ENDLOOP;
(3)PL/SQL 中嵌套的 WHILELOOP 循环语句的语法:
WHILE condition1 LOOP
sequence_of_statements1
WHILE condition2 LOOP
sequence_of_statements2
ENDLOOP;ENDLOOP;
------九九乘法表------------利用for循环:DECLARE
x INT :=1;
y INT :=1;BEGINFOR x IN1..9LOOPFOR y IN1.. x LOOP
dbms_output.put(y ||' x '|| x ||' = '|| x * y ||' ');ENDLOOP;
dbms_output.put_line(' ');ENDLOOP;END;-----利用while:DECLARE
x INT :=1;
y INT;BEGINWHILE x <=9LOOP
y :=1;WHILE y <= x LOOP
dbms_output.put(y ||' x '|| x ||' = '|| x*y ||' ');
y := y +1;ENDLOOP;
x := x +1;
dbms_output.put_line('');ENDLOOP;END;