Good day.
I create a Duty schedule application for our technical support hotline, using Qt + MySQL.
For each month it is necessary to monthly fill in the calendar table containing the date and sign of the day off or working day..
The table structure is as follows:
CREATE TABLE `calendar` ( `calendarID` int(10) unsigned NOT NULL AUTO_INCREMENT, `monthID` int(10) unsigned NOT NULL, `date` date NOT NULL, `iswork` tinyint(3) unsigned NOT NULL, PRIMARY KEY (`calendarID`), KEY `month` (`monthID`) ) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8mb4
The procedure was written without checking incoming values.
February leap year handles correctly.
Called
call filling_the_calendar(2,2016);
CREATE DEFINER=`root`@`localhost` PROCEDURE `filling_the_calendar`(IN cur_month INT, cur_year INT) BEGIN -- We declare variables the current day, the first day of the month, the first day of the next month. DECLARE cur_day, first_day, next_month DATE; --Month ID, working day or not DECLARE monthID, iswork INT; /*Variable initialization*/ SET first_day = CONCAT(cur_year,'-',cur_month,'-01'); SET monthID = cur_year * 100 + cur_month; SET next_month = DATE_ADD(first_day, INTERVAL 1 MONTH); SET cur_day = first_day; /*Going through all the days of the month */ WHILE cur_day < next_month DO /*If the day of the week is Saturday or Sunday they are not working*/ IF (WEEKDAY(cur_day) = 6 OR WEEKDAY(cur_day) = 5) THEN SET iswork = 0; ELSE SET iswork = 1; END IF; /*add record to table*/ INSERT INTO calendar (`monthID`, `date`, `iswork`) VALUES (monthID, cur_day, iswork); /*Increase the day*/ SET cur_day = DATE_ADD(cur_day, INTERVAL 1 DAY); END WHILE; END