plsql判断和循环


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; ``` 
   


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM