一些数据库的循环方法:
Mysql
- drop
PROCEDURE
if
exists myLoop; - create
PROCEDURE myLoop(begin_date varchar(10), end_date varchar(10)) - begin
- ????declare v_date datetime;
- ????declare v_begin_date, v_end_date varchar(10);
- ????set v_date = begin_date;
- ????while v_date < end_date do
- ????????set v_begin_date = DATE_FORMAT(v_date,‘%Y-%m-%d‘);
- ????????set v_date = DATE_ADD(v_date, INTERVAL 1 DAY);
- ????????set v_end_date = DATE_FORMAT(v_date,‘%Y-%m-%d‘);
- ????????select v_begin_date, v_end_date;
- ????????-- write your sql here!
- ????end
while; - end;
- -- call myLoop(‘2013-08-15‘,‘2013-09-02‘);
?
?
Oracle
- declare
- ???????v_date date;
- ???????v_begin_date varchar2(10);
- ???????v_end_date varchar2(10);
- begin
- ???????v_date := date‘2014-06-02‘;
- ???????while v_date < date‘2014-06-20‘ loop
- ?????????????v_begin_date := to_char(v_date, ‘yyyy-mm-dd‘);
- ?????????????v_date := v_date + 1;
- ?????????????v_end_date := to_char(v_date, ‘yyyy-mm-dd‘);
- ?????????????dbms_output.put_line(v_begin_date||‘ === ‘||v_end_date);
- ?????????????-- write your sql here!
- ?????????end loop;
- ?????????commit;
- end;
时间: 2024-11-01 15:33:45