Thursday, March 7, 2013

How to create a mysql procedure


DROP PROCEDURE IF EXISTS mydatabase.datespopulate;

DELIMITER |
CREATE PROCEDURE mydatabase.datespopulate(dateStart DATE, dateEnd DATE)
BEGIN
  WHILE dateStart <= dateEnd DO
     INSERT INTO mydatabase.datetable (d) VALUES (dateStart);
    SET dateStart = date_add(dateStart, INTERVAL 1 DAY);
  END WHILE;
END;

DELIMITER ;
CALL mydatabase.datespopulate('2013-02-01','2015-06-30');

delete from mydatabase.datetable;

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.