当前位置: 移动技术网 > IT编程>数据库>Oracle > PL/SQL 条件控制语句

PL/SQL 条件控制语句

2018年08月28日  | 移动技术网IT编程  | 我要评论

过水漫游沙皮,中国智能交通,溺生长下

1、if-then 语句

语法:

if 条件 then
     语句序列;
end if;

实例:

declare 
   i number(2) := 10; 
begin 
   if i < 20 then
     dbms_output.put_line('true');
   end if;
end; 

2、if-then-else 语句

语法:

if 条件 then
     语句序列1;
else
     语句序列2;
end if;

实例:

declare 
   i number(2) := 10; 
begin 
   if i < 10 then
     dbms_output.put_line('true');
   else
     dbms_output.put_line('false');
   end if;
end; 

3、if-then-elsif 语句

语法:

if 条件1 then
     语句序列1;
elsif 条件2 then
     语句序列2;
else
     语句序列3;
end if;

实例:

declare 
   i number(2) := 10; 
begin 
   if i < 10 then
     dbms_output.put_line('true1');
   elsif i < 20 then
     dbms_output.put_line('true2');
   else
     dbms_output.put_line('false');
   end if;
end; 

注:可以在一个if-thenif-then-elsif语句中使用另一个if-thenif-then-elsif语句。

4、case 语句

语法:

case selector 
   when 'value1' then 语句序列1; 
   when 'value2' then 语句序列2; 
   when 'value3' then 语句序列3; 
   ... 
   else 语句序列n;  -- default case 
end case;

实例:

declare
  sex char(1) := '1';
begin
  case sex 
    when '1' then dbms_output.put_line('男'); 
    when '2' then dbms_output.put_line('女');   
    else dbms_output.put_line('ry');  
  end case;
end;

5、搜索 case 语句

语法:

case 
   when selector = 'value1' then 语句序列1; 
   when selector = 'value2' then 语句序列2; 
   when selector = 'value3' then 语句序列3; 
   ... 
   else 语句序列n;  -- default case 
end case;

实例:

declare
  sex char(1) := '1';
begin
  case 
    when sex = '1' then dbms_output.put_line('男'); 
    when sex = '2' then dbms_output.put_line('女');   
    else dbms_output.put_line('ry');  
  end case;
end;

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网