oracle 控制结构

1.if 逻辑结构

  if/then 结构是最简单的条件测试,如果条件为真,则执行程序的一行或者多行,如果条件为假,则什么都不执行,

 示例:

   if  1>2 then

       null;

   end if ;

  if not 1<2 then

      null;

  end if ;

  if/then / else 简单的if/then 结构类似,差别在于,如果条件为false,则执行else 后面的语句。

  示例:

   if 1>2 then

       null;

   else

     null;

  end if ;

  if /then /elsif  ,在这中结构,在条件为false 是可以对另一个条件测试,因此,不用嵌套if语句也可以对多个条件进行测试

  示例;

   if  1>2  then

      null;

   elsif  1>3

     null;

   else

     null;

   end if ;

 备注;每一个if语句块必须至少有一行程序代码,如果不希望在程序代码中做任何事情,则简单的使用null;命令即可。

         对每一个elsif 语句没有匹配的end if语句。

2 case 语句

   语句格式;

   case   variable

        when  expression1  then value1

        when  expression2 then value2

        when  expression2 then value3

        else  value4

  end ;

  示例:

   declare

               val  varchar2(100);

               city   varchar2(20) :='TORONTD';

   begin

                val:= case city

                 when  'TOROND'  then 'RA:PTORS'

                 when 'LOS ANGELES' then 'LAKERS'

                 else 'NO TEAM'

  end ;

3.loop 循环

  loop 格式;

     loop 

           executeable statements;

   end loop;

  示例:

     loop 

           if   l_bank_balance >=0  then exit;

           else

                  l_decision ;='ACCOUNT OVERDRAWN';

          end if ;

    end loop;

4 while 循环

  while 循环就会一直循环下去,当条件变为假时跳出循环,如果条件永远为真就不能满足。就会一直循环下去而不会跳出循环。

 示例:

  while   scount<10 loop

        scount=scount+1;

  end loop;

5 for 循环

  循环次数进行控制

  格式;

  for   l_count in 1 ... 10

 loop

    statements;

 end loop;

 备注:变量l_count 的 值在1到10之间, (...) 就是说这个变量在这个两个数之间计数。

 示例:

   declare

              l_count  number ;

    begin

        for  l_count in  1 ... 5

        loop

         dbms_output.put_line(l_count);

        end loop; 

    end ;

 

 

   

 

 

 

posted @ 2013-09-21 22:33  sulin  阅读(390)  评论(0)    收藏  举报