oracle控制语句if else,loop等
2017-09-12 23:37
555 查看
oracle中的控制语句
1.条件语句
if xxx then
else
xxx;
end if;
---------------------
if xxx then
elsif xxx then
xxx;
else
xxx;
end if;
---------------------
case
when xxx then
xxx;
when xxx then
xxx;
else
end case;
---------------------
case xxx
when xxx then
xxx;
when xxx then
xxx;
else
end case;
---------------------
select student_name,
sum(case when subject='数学' then score else 0 end) 数学,
sum(case when subject='语文' then score else 0 end) 语文,
sum(case when subject='外语' then score else 0 end) 外语
from scores
group by student_name;
2.循环语句
【loop】
--exit when
declare
v_rlt number(8):=-3;
begin
<<fst_loop>>
loop
dbms_output.put_line('v_rlt = '||v_rlt);
v_rlt:=v_rlt+1;
exit fst_loop when v_rlt > 3;
end loop;
dbms_output.put_line('LOOP循环已经结束!');
end;
--结果
v_rlt = -3
v_rlt = -2
v_rlt = -1
v_rlt = 0
v_rlt = 1
v_rlt = 2
v_rlt = 3
LOOP循环已经结束!
--if exit
declare
v_rlt number(8):=-3;
begin
<<fst_loop>>
loop
dbms_output.put_line('v_rlt = '||v_rlt);
v_rlt:=v_rlt+1;
if v_rlt > 3 then
dbms_output.put_line('变量的值已经大于3,当前值为'||v_rlt);
exit fst_loop;
end if;
end loop fst_loop;
dbms_output.put_line('LOOP循环已经结束!');
end;
【while...loop】
--while...loop
declare
v_rlt number(8):=-3;
begin
<<while_loop>>
while(v_rlt < 4)
loop
dbms_output.put_line('v_rlt = '||v_rlt);
v_rlt:=v_rlt+1;
end loop while_loop;
dbms_output.put_line('WHILE循环已经结束!');
end;
【for...loop】
--for..loop
begin
for v_rlt in -3..3 loop
dbms_output.put_line('v_rlt = '||v_rlt);
end loop;
dbms_output.put_line('FOR循环已经结束!');
end;
1.条件语句
if xxx then
else
xxx;
end if;
---------------------
if xxx then
elsif xxx then
xxx;
else
xxx;
end if;
---------------------
case
when xxx then
xxx;
when xxx then
xxx;
else
end case;
---------------------
case xxx
when xxx then
xxx;
when xxx then
xxx;
else
end case;
---------------------
select student_name,
sum(case when subject='数学' then score else 0 end) 数学,
sum(case when subject='语文' then score else 0 end) 语文,
sum(case when subject='外语' then score else 0 end) 外语
from scores
group by student_name;
2.循环语句
【loop】
--exit when
declare
v_rlt number(8):=-3;
begin
<<fst_loop>>
loop
dbms_output.put_line('v_rlt = '||v_rlt);
v_rlt:=v_rlt+1;
exit fst_loop when v_rlt > 3;
end loop;
dbms_output.put_line('LOOP循环已经结束!');
end;
--结果
v_rlt = -3
v_rlt = -2
v_rlt = -1
v_rlt = 0
v_rlt = 1
v_rlt = 2
v_rlt = 3
LOOP循环已经结束!
--if exit
declare
v_rlt number(8):=-3;
begin
<<fst_loop>>
loop
dbms_output.put_line('v_rlt = '||v_rlt);
v_rlt:=v_rlt+1;
if v_rlt > 3 then
dbms_output.put_line('变量的值已经大于3,当前值为'||v_rlt);
exit fst_loop;
end if;
end loop fst_loop;
dbms_output.put_line('LOOP循环已经结束!');
end;
【while...loop】
--while...loop
declare
v_rlt number(8):=-3;
begin
<<while_loop>>
while(v_rlt < 4)
loop
dbms_output.put_line('v_rlt = '||v_rlt);
v_rlt:=v_rlt+1;
end loop while_loop;
dbms_output.put_line('WHILE循环已经结束!');
end;
【for...loop】
--for..loop
begin
for v_rlt in -3..3 loop
dbms_output.put_line('v_rlt = '||v_rlt);
end loop;
dbms_output.put_line('FOR循环已经结束!');
end;
相关文章推荐
- Oracle中的几种控制语句if..else,for,loop,while
- oracle的控制语句if和循环语句loop while for
- Oracle控制语句(if , loop , while , for)
- Oracle控制语句(if , loop , while , for)
- Oracle控制语句(if , loop , while , for)
- Oracle控制语句(if , loop , while , for)
- Oracle控制语句(if , loop , while , for)
- java开发C语言编译器:消除冗余语句和把ifelse控制语句编译成字节码
- python流程控制语句 ifelse - 1
- Oracle PL/SQL之LOOP循环控制语句
- Oracle PL/SQL之LOOP循环控制语句
- Oracle PL/SQL之LOOP循环控制语句
- 反汇编之流程控制语句的识别(if...else...语句)
- Python流程控制语句->if .......else
- PHP流程控制语句-if、if-else、if-elseif-else语句
- SQL 流程控制语句 之一 IF…ELSE语句
- java学习之路 之 基本语法-程序流程控制-(if-else)语句练习题
- oracle PL/SQL(procedure language/SQL)程序设计--控制结构(if else )
- Python流程控制语句->if .......else
- PHP分支控制语句 if...else 和 switch