if-then 结构
仅当条件值为TRUE时,才执行语句序列
如果条件判定为FALSE 或者NULL,则不执行任何操作
语法
if then
语句;
end if;
declare
i int;
begin
i:='&shuru';
if i=5 then
dbms_output.put_line(i+5);
else
dbms_output.put_line(i);
end if;
end;
if-then-else 结构
仅当条件值为FALSE或者null,才执行else
子句中的语句序列
语法
if then
语句;
else
语句;
end if;
declare
i int;
begin
i:='&shuru';
if i=5 then
dbms_output.put_line(i+5);
else
dbms_output.put_line(i);
end if;
end;
--用if-then-elsif语句设计输出不同的值
declare
i int;
begin
i:=8;
if i<5 then
dbms_output.put_line('i<5');
elsif i=5 then
dbms_output.put_line('I等于5');
else
dbms_output.put_line('I大于5');
end if;
end;
--case 语句单一选择等值比较示例
declare
n int;
begin
n:=&n;
case n
when 1 then
dbms_output.put_line('n=1');
when 2 then
dbms_output.put_line('n=2');
when 3 then
dbms_output.put_line('n=3');
when 4 then
dbms_output.put_line('n=4');
else
dbms_output.put_line('n='||to_char(n));
end case;
end;
--case 语句多条件比较示例
declare
n int;
begin
n:=&n;
case
when n<50 then
dbms_output.put_line('n小于50');
when n>50 then
dbms_output.put_line('n大于50');
else
dbms_output.put_line('n等于'||to_char(n));
end case;
end;
--循环控制
使用loop 或者goto语句可以重复或者跳出过程序块的选取部分
有三种形式的LOOP语句
LOOP
WHILE LOOP
FOR LOOP
LOOP语句
语法
loop
语句;
exit
exit-when
end loop;
被重复的语句位于关键词LOOP 和END LOOP 之间
每执行一次循环迭代过程,就执行一次语句序列,然后控制又回到循环开始位置
exit 语句
如果不需要或不可能让循环进一步处理,用于完成循环
有两种形式的EXIT语句
exit
exit when
强制循环无条件完成
必须放在循环内
LOOP
语句;
if THEN
EXIT; --立刻退出循环
END IF;
END LOOP; ---控制到此处恢复
示例 计算S=1+3+5+...+99
declare
i int:=1;
s int:=0;
begin
loop
s:=s+i;
i:=i+2;
if i>99 then
exit;
end if ;
end loop;
dbms_output.put_line(s);
end;
exit -when 语句
允许循环有条件完成
在遇到exit 语句时对 WHEN 子句中条件进行判断
如果判定条件为TRUE 则循环完成
LOOP
语句;
exit when ;
end loop;
计算 s=5!
declare
i int:=1;
s int:=1;
begin
loop
s:=s*i;
i:=i+1;
exit when i>5;
end loop;
dbms_output.put_line('5!='||to_char(s));
end;
while loop 语句
语法
WHILE
LOOP
语句;
END LOOP;
将条件与 LOOP-END LOOP 中的语句序列相关联
循环执行的次数取决于条件且只有完成循环后才可知
WHILE-LOOP 示例: 计算N!
declare
i int :=1;
n int;
k number:=1;
begin
n:=&n;
while i<=n loop
k:=k*i;
i:=i+1;
end loop;
dbms_output.put_line(to_char(n)||'!='||to_char(k));
end;
for -loop语句
语句
for in
loop
语句;
end loop;
循环在指定的整数范围内进行
不能为循环计数器赋值
在运行时可以动态分配循环范围
对于一定范围内的每个整数,都执行一次该语句
每执行一次循环,循环计数器就会增加
for loop 示例: 计算N!
declare
i int;
n int;
k number:=1;
begin
n:=&n;
for i in 1..n
loop
k:=k*i;
end loop;
dbms_output.put_line(to_char(n)||'!='||to_char(k));
end;
S=1!+2!+3!+...N!
declare
m int;
s number:=0;
k number:=1;
begin
m:=&m;
for i in 1..m
loop
k:=k*i;
s:=s+k;
end loop;
dbms_output.put_line('1!+2!+3!+...+n!='||to_char(s));
end;
---把岗位是SALESMAN的用户展示出来
select * FROM SCOTT.EMP
declare
cursor mycur is(
select * from scott.emp where scott.emp.job = 'SALESMAN');
myrec scott.emp%rowtype;
begin
open mycur;
fetch mycur
into myrec;
while mycur%found loop
dbms_output.put_line(myrec.empno || ', ' || myrec.job ||
', ' || myrec.sal);
fetch mycur
into myrec;
end loop;
close mycur;
end;
goto 语句
无条件跳转到一个标签
执行时,语句将更改PL/SQL程序块的控制流
编制GOTO语句代码需要有两个部分
定义标签名称
使用GOTO语句将控制转到标签
在下列位置可以使用GOTO语句来转移控制
从程序块到可执行语句
从异常处理程序分支到封闭的程序块
标签名称
可以选择将其用于命名PL/SQL程序块或者程序块中的语句
使用尖括号(<< >>)进行定义
<>
if 条件 then
语句;
goto if_fare_label;
不允许在下列位置使用goto语句转移控制
从某个IF语句或循环子句内转到其他语句
从封闭程序块转到当前程序块
从异常处理程序转到当前程序块
子程序之外
转到关键字
NULL语句
明确指定不进行操作(例如,除了将控制转移到下一个语句而不进行任何操作)
他提高了可读性
IF newfare>90 then
语句;
else
null;
end if;
注释的使用
添加注释可以提高程序的可读性并帮助理解
PL/SQL 支持两种注释样式
单行注释
可以在行中的任何地方以双横杠(--) 开始
可以扩展到行尾
多行注释
这些注释以/*开始并以*/结束
可以跨越多行
仅当条件值为TRUE时,才执行语句序列
如果条件判定为FALSE 或者NULL,则不执行任何操作
语法
if then
语句;
end if;
declare
i int;
begin
i:='&shuru';
if i=5 then
dbms_output.put_line(i+5);
else
dbms_output.put_line(i);
end if;
end;
if-then-else 结构
仅当条件值为FALSE或者null,才执行else
子句中的语句序列
语法
if then
语句;
else
语句;
end if;
declare
i int;
begin
i:='&shuru';
if i=5 then
dbms_output.put_line(i+5);
else
dbms_output.put_line(i);
end if;
end;
--用if-then-elsif语句设计输出不同的值
declare
i int;
begin
i:=8;
if i<5 then
dbms_output.put_line('i<5');
elsif i=5 then
dbms_output.put_line('I等于5');
else
dbms_output.put_line('I大于5');
end if;
end;
--case 语句单一选择等值比较示例
declare
n int;
begin
n:=&n;
case n
when 1 then
dbms_output.put_line('n=1');
when 2 then
dbms_output.put_line('n=2');
when 3 then
dbms_output.put_line('n=3');
when 4 then
dbms_output.put_line('n=4');
else
dbms_output.put_line('n='||to_char(n));
end case;
end;
--case 语句多条件比较示例
declare
n int;
begin
n:=&n;
case
when n<50 then
dbms_output.put_line('n小于50');
when n>50 then
dbms_output.put_line('n大于50');
else
dbms_output.put_line('n等于'||to_char(n));
end case;
end;
--循环控制
使用loop 或者goto语句可以重复或者跳出过程序块的选取部分
有三种形式的LOOP语句
LOOP
WHILE LOOP
FOR LOOP
LOOP语句
语法
loop
语句;
exit
exit-when
end loop;
被重复的语句位于关键词LOOP 和END LOOP 之间
每执行一次循环迭代过程,就执行一次语句序列,然后控制又回到循环开始位置
exit 语句
如果不需要或不可能让循环进一步处理,用于完成循环
有两种形式的EXIT语句
exit
exit when
强制循环无条件完成
必须放在循环内
LOOP
语句;
if THEN
EXIT; --立刻退出循环
END IF;
END LOOP; ---控制到此处恢复
示例 计算S=1+3+5+...+99
declare
i int:=1;
s int:=0;
begin
loop
s:=s+i;
i:=i+2;
if i>99 then
exit;
end if ;
end loop;
dbms_output.put_line(s);
end;
exit -when 语句
允许循环有条件完成
在遇到exit 语句时对 WHEN 子句中条件进行判断
如果判定条件为TRUE 则循环完成
LOOP
语句;
exit when ;
end loop;
计算 s=5!
declare
i int:=1;
s int:=1;
begin
loop
s:=s*i;
i:=i+1;
exit when i>5;
end loop;
dbms_output.put_line('5!='||to_char(s));
end;
while loop 语句
语法
WHILE
LOOP
语句;
END LOOP;
将条件与 LOOP-END LOOP 中的语句序列相关联
循环执行的次数取决于条件且只有完成循环后才可知
WHILE-LOOP 示例: 计算N!
declare
i int :=1;
n int;
k number:=1;
begin
n:=&n;
while i<=n loop
k:=k*i;
i:=i+1;
end loop;
dbms_output.put_line(to_char(n)||'!='||to_char(k));
end;
for -loop语句
语句
for in
loop
语句;
end loop;
循环在指定的整数范围内进行
不能为循环计数器赋值
在运行时可以动态分配循环范围
对于一定范围内的每个整数,都执行一次该语句
每执行一次循环,循环计数器就会增加
for loop 示例: 计算N!
declare
i int;
n int;
k number:=1;
begin
n:=&n;
for i in 1..n
loop
k:=k*i;
end loop;
dbms_output.put_line(to_char(n)||'!='||to_char(k));
end;
S=1!+2!+3!+...N!
declare
m int;
s number:=0;
k number:=1;
begin
m:=&m;
for i in 1..m
loop
k:=k*i;
s:=s+k;
end loop;
dbms_output.put_line('1!+2!+3!+...+n!='||to_char(s));
end;
---把岗位是SALESMAN的用户展示出来
select * FROM SCOTT.EMP
declare
cursor mycur is(
select * from scott.emp where scott.emp.job = 'SALESMAN');
myrec scott.emp%rowtype;
begin
open mycur;
fetch mycur
into myrec;
while mycur%found loop
dbms_output.put_line(myrec.empno || ', ' || myrec.job ||
', ' || myrec.sal);
fetch mycur
into myrec;
end loop;
close mycur;
end;
goto 语句
无条件跳转到一个标签
执行时,语句将更改PL/SQL程序块的控制流
编制GOTO语句代码需要有两个部分
定义标签名称
使用GOTO语句将控制转到标签
在下列位置可以使用GOTO语句来转移控制
从程序块到可执行语句
从异常处理程序分支到封闭的程序块
标签名称
可以选择将其用于命名PL/SQL程序块或者程序块中的语句
使用尖括号(<< >>)进行定义
<>
if 条件 then
语句;
goto if_fare_label;
不允许在下列位置使用goto语句转移控制
从某个IF语句或循环子句内转到其他语句
从封闭程序块转到当前程序块
从异常处理程序转到当前程序块
子程序之外
转到关键字
NULL语句
明确指定不进行操作(例如,除了将控制转移到下一个语句而不进行任何操作)
他提高了可读性
IF newfare>90 then
语句;
else
null;
end if;
注释的使用
添加注释可以提高程序的可读性并帮助理解
PL/SQL 支持两种注释样式
单行注释
可以在行中的任何地方以双横杠(--) 开始
可以扩展到行尾
多行注释
这些注释以/*开始并以*/结束
可以跨越多行
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29828180/viewspace-1258742/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29828180/viewspace-1258742/