• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

fill a table with stored proceedure Year and month mysql

I am using a stored proceedure to fill a table - I want to fill it with year and month
so if the begining date is 2012-01-01 and the end date is 2012 - 03-01
I want it to fill the table like so:
Yr       |  Mo
2012  | 01
2012  | 02
2012  | 03

I wrote this but it does ths

Yr       |  Mo
2012  | 01
2012  | 01
2012  | 01

DELIMITER |
CREATE PROCEDURE fill_calendar(start_date DATE, end_date DATE)
BEGIN
  DECLARE crt_date DATE;
  DECLARE Yr INT;
  DECLARE Mo INT;
  SET crt_date = start_date;
  SET Yr= Year(start_date);
  SET Mo= Month(start_date);
  
  WHILE crt_date < end_date DO
    INSERT INTO calendar(Yr,Mo) VALUES(Yr,Mo);
    SET crt_date = ADDDATE(crt_date, INTERVAL 1 MONTH);
  END WHILE;
END |
DELIMITER ;

Open in new window

0
r3nder
Asked:
r3nder
  • 2
1 Solution
 
rjohnsonjrCommented:
You need to put these in your while loop before the insert...
SET Yr= Year(crt_date );
  SET Mo= Month(crt_date );
0
 
r3nderAuthor Commented:
I realized that after I put in the question :D Thanks rjohnsonjr
0
 
r3nderAuthor Commented:
Thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now