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=80 and test_scores=70 and test_scores=60 and test_scores

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

goto(不常用)

执行到goto语句时,代码会回到标签所在位置,重新往下执行


declare
  --声明一个变量作为循环变量
  n number:=1;
begin
  <<lebal>>
  dbms_output.put_line(n);
  n:=n+1;
  if n<11 then
    goto lebal;
  end if;
end;

版权声明:本文为inmeditation原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/inmeditation/p/12018958.html