Solved

fill a table with stored proceedure Year and month mysql

Posted on 2015-02-13
3
229 Views
Last Modified: 2015-02-13
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
Comment
Question by:r3nder
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
rjohnsonjr earned 500 total points
ID: 40608008
You need to put these in your while loop before the insert...
SET Yr= Year(crt_date );
  SET Mo= Month(crt_date );
0
 
LVL 6

Author Comment

by:r3nder
ID: 40608075
I realized that after I put in the question :D Thanks rjohnsonjr
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 40608078
Thanks!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question