Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

fill a table with stored proceedure Year and month mysql

Posted on 2015-02-13
3
Medium Priority
?
237 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
[X]
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
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
rjohnsonjr earned 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

618 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