Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

fill a table with stored proceedure Year and month mysql

Posted on 2015-02-13
3
228 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

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'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

790 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