一个Oracle循环例子

来源:互联网 发布:php设计表格 编辑:程序博客网 时间:2024/06/09 17:32
  1. DECLARE  
  2.   v_index     NUMBER(2);  
  3.   v_tmp       VARCHAR2(10);  
  4.   v_month     VARCHAR2(10);  
  5.   v_yearmonth VARCHAR2(10);  
  6.   v_result    VARCHAR2(50);  
  7.   v_year      VARCHAR2(10);  
  8.   v_maxMonth  NUMBER(2);  
  9. BEGIN  
  10.    v_year := '';  
  11.    v_index := 0;  
  12.    v_result := '';  
  13.    LOOP  
  14.       v_index := v_index + 1;     
  15.         
  16.       if v_index>26 then  
  17.          exit;  
  18.       end if;  
  19.         
  20.       if (v_index < 13) then  
  21.          v_month := v_index;  
  22.       elsif (v_index > 12 and v_index < 25) then  
  23.          v_month := to_char(v_index - 12);         
  24.       elsif (v_index > 24) then  
  25.         v_month := to_char(v_index - 24);  
  26.       end if;  
  27.         
  28.       if (length(v_month) < 2) then  
  29.          v_month := '0' || to_char(v_month);         
  30.       else  
  31.         v_month := to_char(v_month);  
  32.       end if;              
  33.         
  34.       if (v_index > 12 and v_index < 25)  then  
  35.          v_year := '2013';       
  36.       elsif (v_index > 24) then    
  37.          v_year := '2014';     
  38.       else  
  39.          v_year := '2012';   
  40.       end if;         
  41.         
  42.       v_yearmonth := v_year || to_char(v_month);   
  43.        
  44.       dbms_output.put_line(v_yearmonth);   
  45.    END LOOP;   
  46. END;  


结果:

201201
201202
201203
201204
201205
201206
201207
201208
201209
201210
201211
201212
201301
201302
201303
201304
201305
201306
201307
201308
201309
201310
201311
201312
201401
201402

0 0