小菜毛毛技術分享

          與大家共同成長

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            164 Posts :: 141 Stories :: 94 Comments :: 0 Trackbacks
          一、條件分支語句
          條件分支語句用于依據特定的情況選擇要執行的操作,PL/SQL提供了三種條件分支語句:if-then, if-then-else,if-then-elsif。
          語法如下:
          Oracle代碼 復制代碼
          1. if conditions then   
          2.    statements;   
          3. [elseif conditions then    
          4.    statements;]   
          5. [else    
          6.    statements;]   
          7. end if;  

             1、if-then示例
              用于執行單一條件判斷,如果滿足特定條件則會執行相應操作,如果不滿足特定條件則退出條件分支語句。
            
          Oracle代碼 復制代碼
          1.     
          2.  declare   
          3.    v_count number;   
          4.  begin   
          5.  select count(*) into v_count from cip_temps;   
          6.  if(v_count>0) then   
          7.  dbms_output.put_line('v_cont的值:'|| v_count);   
          8.    end if;   
          9.  end;   
          10. /   

              2、if-then-else示例
               用于執行二重條件判斷,如果滿足特定條件則執行一組操作,如果不滿足則執行另一組操作。
               
          Oracle代碼 復制代碼
          1.  declare   
          2.    v_count number;   
          3.  begin   
          4.    select count(*) into v_count from cip_temps;   
          5.      if(v_count>11) then   
          6.   dbms_output.put_line('v_cont的值:'|| v_count);   
          7.      else    
          8.      dbms_output.put_line('v_count的值:'|| v_count);   
          9.    end if;   
          10.  end;   
          11. /   

            
              3、if-then-elsif示例
               用于執行多重條件判斷,如果滿足特定條件1則執行第一組操作,如果滿足特定條件2則執行第二組操作,以此類推,如果都不滿足特定條件則執行不滿足條件的操作。
             
          Oracle代碼 復制代碼
          1. declare   
          2.   v_count number;   
          3. begin   
          4.   select count(*) into v_count from cip_temps;   
          5.   if(v_count>10) then   
          6.     dbms_output.put_line('if操作___v_cont的值:'|| v_count);   
          7.   elsif (v_count=10) then   
          8.     dbms_output.put_line('elsif操作____v_count的值:'|| v_count);   
          9.   else   
          10.     dbms_output.put_line('else操作____v_cout的值:'||v_count);   
          11.   end if;   
          12. end;   
          13. /    

          二、case語句
          當執行多重條件分支語句時,使用case語句更加簡潔、而且效率也更好。case語句處理多重條件分支語句有兩種方法,第一種方法是使用單一選擇符進行等值比較。第二種方法是使用多種條件進行非等值比較。
             1、使用單一選擇符進行等值比較
                當執行case語句執行多重條件分支時,如果條件選擇符完全相同,并且條件表達式為相同條件選擇,那么可以選擇單一選擇符進行等值比較,語法如下:
            
          Oracle代碼 復制代碼
          1. case  條件選擇符   
          2.    when   條件值表達式1 then 要執行的操作1;   
          3.    when   條件值表達式2 then 要執行的操作2;   
          4.     。。。。。。。   
          5.    else    
          6.      要執行的操作。   
          7.    end case;   

            示例如下:
             
          Oracle代碼 復制代碼
          1. declare   
          2.   v_count number;   
          3. begi   
          4.  select count(*) into v_count from cip_temps;   
          5.   case v_count   
          6.     when 1 then   
          7.       dbms_output.put_line('when 1操作___v_cont的值:'|| v_count);   
          8.     when 5 then   
          9.      dbms_output.put_line('when 5操作___v_count的值:'|| v_count);   
          10.     when 10 then   
          11.      dbms_output.put_line('when 10操作____v_count的值:'|| v_count);   
          12.   else   
          13.     dbms_output.put_line('else操作____v_cout的值:'||v_count);   
          14.  end case;   
          15. end;   
          16. /    

             2、case使用多種條件進行比較
             如果選擇多個條件進行不同比較時,那么必須在when子句中指定比較條件,語法如下:
              
          Oracle代碼 復制代碼
          1. case     
          2.    when   條件值表達式1 then 要執行的操作1;   
          3.    when   條件值表達式2 then 要執行的操作2;   
          4.     。。。。。。。   
          5.    else    
          6.      要執行的操作。   
          7.    end case;   

            示例如下:
            
          Oracle代碼 復制代碼
          1. declare   
          2.   v_count number;   
          3. begin   
          4.  select count(*) into v_count from cip_temps;   
          5.   case    
          6.     when v_count>10 then   
          7.       dbms_output.put_line('when 1操作___v_cont的值:'|| v_count);   
          8.     when v_count>5 then   
          9.      dbms_output.put_line('when 5操作___v_count的值:'|| v_count);   
          10.     when v_count>4 then   
          11.      dbms_output.put_line('when 10操作____v_count的值:'|| v_count);   
          12.     else   
          13.     dbms_output.put_line('else操作____v_cout的值:'||v_count);   
          14.  end case;   
          15. end;   
          16. /    

          三、循環語句
            三種循環:基本循環、while循環、for循環語句。
          1、基本循環
          循環語句以loop開始,以end loop結束,其語法如下:
            loop
                statement1;
                 。。。。。。
                 exit[when condition];
              end loop;
          注意:當執行基本循環語句時,無論是否滿足條件,語句至少會被執行一次,當condition為true時,會推出循環。并執行end loop后的相應操作。當編寫基本循環語句時,一定要有exit語句,否則會出現死循環,另外還要定義循環控制變量。
            示例如下:
          Oracle代碼 復制代碼
          1.  declare   
          2.   i int:=1;   
          3.  begin   
          4.   loop      
          5. dbms_output.put_line(i);   
          6. exit when i=10;   
          7. i:=i+1;   
          8. nd loop;   
          9.  end;   
          10.  /   

          2、while循環
          對于while循環,只要條件為true時,才執行循環體內語句。while循環以while...loop開始,以end loop結束。其語法如下:

          while 條件 loop
          語句1;
          語法2;
          end loop;

          當條件為true執行循環體內語句,當條件為false或full時,則跳出循環執行end loop以后的語句,另外還要定義循環控制變量。
          示例如下:
          Oracle代碼 復制代碼
          1.   declare   
          2.    i int:=1;   
          3.   begin   
          4.    while i<=10 loop      
          5.   dbms_output.put_line(i);   
          6.  i:=i+1;   
          7. end loop;   
          8.   end;   
          9.   /   

          3、for循環
          當使用for循環時,oracle會隱藏定義循環控制變量(即不需要人工定義循環控制變量),語法如下:

          for counter in[reverse]
          lower_bound..upper_bound loop
                statement1;
                statement2;
                ..........
          end loop;

          注意:counter循環控制變量,oracle會隱藏定義,lower_bound和upper_bound分別對應于循環變量的下界值和上界值,默認情況下,當使用for循環時,每次循環時循環控制變量會自動增一;如果指定reverse選項,那么每次循環時循環變量會自動減一。示例如下:
          Oracle代碼 復制代碼
          1. declare   
          2.  begin   
          3.  for i in reverse  1..10 loop   
          4.   dbms_output.put_line(i);   
          5.  end loop;   
          6.  end;   
          7.  /    
          posted on 2009-08-20 20:29 小菜毛毛 閱讀(500) 評論(0)  編輯  收藏 所屬分類: 數據庫
          主站蜘蛛池模板: 古田县| 北安市| 乐山市| 贵德县| 垫江县| 新巴尔虎右旗| 二连浩特市| 永济市| 无棣县| 永德县| 淅川县| 青神县| 达孜县| 天峨县| 花莲市| 宁河县| 乌鲁木齐市| 广德县| 启东市| 科技| 昂仁县| 山东省| 双鸭山市| 沂水县| 长武县| 马关县| 德州市| 扎兰屯市| 辽宁省| 公安县| 元谋县| 罗田县| 祁门县| 扶绥县| 鸡西市| 白玉县| 镇巴县| 宁蒗| 观塘区| 普兰店市| 汨罗市|