首页 > 代码库 > 一些数据库的循环方法

一些数据库的循环方法

一些数据库的循环方法:

Mysql

  1. drop PROCEDURE if exists myLoop;
  2. create PROCEDURE myLoop(begin_date varchar(10), end_date varchar(10))
  3. begin
  4. ????declare v_date datetime;
  5. ????declare v_begin_date, v_end_date varchar(10);
  6. ????set v_date = begin_date;
  7. ????while v_date < end_date do
  8. ????????set v_begin_date = DATE_FORMAT(v_date,‘%Y-%m-%d‘);
  9. ????????set v_date = DATE_ADD(v_date, INTERVAL 1 DAY);
  10. ????????set v_end_date = DATE_FORMAT(v_date,‘%Y-%m-%d‘);
  11. ????????select v_begin_date, v_end_date;
  12. ????????-- write your sql here!
  13. ????end while;
  14. end;
  15. -- call myLoop(‘2013-08-15‘,‘2013-09-02‘);

?

?

Oracle

  1. declare
  2. ???????v_date date;
  3. ???????v_begin_date varchar2(10);
  4. ???????v_end_date varchar2(10);
  5. begin
  6. ???????v_date := date‘2014-06-02‘;
  7. ???????while v_date < date‘2014-06-20‘ loop
  8. ?????????????v_begin_date := to_char(v_date, ‘yyyy-mm-dd‘);
  9. ?????????????v_date := v_date + 1;
  10. ?????????????v_end_date := to_char(v_date, ‘yyyy-mm-dd‘);
  11. ?????????????dbms_output.put_line(v_begin_date||‘ === ‘||v_end_date);
  12. ?????????????-- write your sql here!
  13. ?????????end loop;
  14. ?????????commit;
  15. end;

一些数据库的循环方法