Solved

fill a table with stored proceedure Year and month mysql

Posted on 2015-02-13
3
224 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
Comment Utility
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
Comment Utility
I realized that after I put in the question :D Thanks rjohnsonjr
0
 
LVL 6

Author Closing Comment

by:r3nder
Comment Utility
Thanks!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now