Ruslan Polupan
Жел. 14, 2018, 6:48 Т.Қ.

MySQL сақталған процедуралары

Добрый день

Я создаю приложение Duty Schedule для нашей горячей линии технической поддержки, используя Qt + MySQL.
На каждый месяц необходимо ежемесячно заполнять календарную таблицу, содержащую дату и знак выходного или рабочего дня.


Структура таблицы следующая:

  1. CREATE TABLE `calendar` (
  2. `calendarID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  3. `monthID` int(10) unsigned NOT NULL,
  4. `date` date NOT NULL,
  5. `iswork` tinyint(3) unsigned NOT NULL,
  6. PRIMARY KEY (`calendarID`),
  7. KEY `month` (`monthID`)
  8. ) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8mb4

Процедура была написана без проверки входящих значений.
Февраль високосный год обрабатывается правильно.

Вызов

  1. call filling_the_calendar(2,2016);
  1. CREATE DEFINER=`root`@`localhost` PROCEDURE `filling_the_calendar`(IN cur_month INT, cur_year INT)
  2. BEGIN
  3. -- We declare variables the current day, the first day of the month, the first day of the next month.
  4. DECLARE cur_day, first_day, next_month DATE;
  5. --Month ID, working day or not
  6. DECLARE monthID, iswork INT;
  7. /*Variable initialization*/
  8. SET first_day = CONCAT(cur_year,'-',cur_month,'-01');
  9. SET monthID = cur_year * 100 + cur_month;
  10. SET next_month = DATE_ADD(first_day, INTERVAL 1 MONTH);
  11. SET cur_day = first_day;
  12. /*Going through all the days of the month */
  13. WHILE cur_day < next_month DO
  14. /*If the day of the week is Saturday or Sunday they are not working*/
  15. IF (WEEKDAY(cur_day) = 6 OR WEEKDAY(cur_day) = 5) THEN
  16. SET iswork = 0;
  17. ELSE
  18. SET iswork = 1;
  19. END IF;
  20. /*add record to table*/
  21. INSERT INTO calendar (`monthID`, `date`, `iswork`)
  22. VALUES (monthID, cur_day, iswork);
  23. /*Increase the day*/
  24. SET cur_day = DATE_ADD(cur_day, INTERVAL 1 DAY);
  25. END WHILE;
  26. END

Мақала бойынша сұралады0сұрақтар(лар)

2

Ол саған ұнайды ма? Әлеуметтік желілерде бөлісіңіз!

Пікірлер

Тек рұқсаты бар пайдаланушылар ғана пікір қалдыра алады.
Кіріңіз немесе Тіркеліңіз