您的位置:首页 > 数据库 > Oracle

一个Oracle循环例子

2015-08-24 14:35 519 查看
DECLARE  

  v_index     NUMBER(2);  

  v_tmp       VARCHAR2(10);  

  v_month     VARCHAR2(10);  

  v_yearmonth VARCHAR2(10);  

  v_result    VARCHAR2(50);  

  v_year      VARCHAR2(10);  

  v_maxMonth  NUMBER(2);  
BEGIN  

   v_year := '';  

   v_index := 0;  

   v_result := '';  

   LOOP  

      v_index := v_index + 1;     

        

      if v_index>26 then  

         exit;  

      end if;  

        

      if (v_index < 13) then  

         v_month := v_index;  

      elsif (v_index > 12 and v_index < 25) then  

         v_month := to_char(v_index - 12);         

      elsif (v_index > 24) then  

        v_month := to_char(v_index - 24);  

      end if;  

        

      if (length(v_month) < 2) then  

         v_month := '0' || to_char(v_month);         

      else  

        v_month := to_char(v_month);  

      end if;              

        

      if (v_index > 12 and v_index < 25)  then  

         v_year := '2013';       

      elsif (v_index > 24) then    

         v_year := '2014';     

      else  

         v_year := '2012';   

      end if;         

        

      v_yearmonth := v_year || to_char(v_month);   

       

      dbms_output.put_line(v_yearmonth);   

   END LOOP;   

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: