mysql排程器樣例程式碼
阿新 • • 發佈:2019-02-14
DELIMITER $$
ALTER DEFINER=`tobplan`@`%` EVENT `park_record_backup_event` ON SCHEDULE EVERY 1 DAY STARTS '2017-10-26 02:00:00' ON COMPLETION PRESERVE ENABLE DO BEGIN
DECLARE month_var VARCHAR (200);
DECLARE year_var VARCHAR (200);
DECLARE month_len VARCHAR (200);
DECLARE sql_var VARCHAR (200) DEFAULT NULL;
DECLARE t_error INTEGER DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;
SELECT MONTH (DATE_SUB(CURDATE(),INTERVAL 66 DAY)) INTO month_var;
SELECT YEAR (DATE_SUB(CURDATE(),INTERVAL 66 DAY)) INTO year_var;
SELECT LENGTH(month_var) INTO month_len;
IF month_len = 1 THEN
SELECT
CONCAT(
'INSERT INTO park_record_',
year_var,
'0',
month_var,
' SELECT * FROM `park_record` WHERE CREATE_DATE <= DATE_ADD(CURDATE(), INTERVAL - 66 DAY)',
';'
) INTO sql_var;
ELSE
SELECT
CONCAT(
'INSERT INTO park_record_',
year_var,
month_var,
' SELECT * FROM `park_record` WHERE CREATE_DATE <= DATE_ADD(CURDATE(), INTERVAL - 66 DAY)',
';'
) INTO sql_var;
END IF;
SET @sql_var = sql_var;
PREPARE stmt FROM @sql_var;
START TRANSACTION;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
DELETE
FROM
park_record
WHERE CREATE_DATE <= DATE_ADD(CURDATE(), INTERVAL - 66 DAY) ;
IF t_error = 1 THEN
ROLLBACK;
ELSE
COMMIT;
END IF;
END$$
ALTER DEFINER=`tobplan`@`%` EVENT `park_record_backup_event` ON SCHEDULE EVERY 1 DAY STARTS '2017-10-26 02:00:00' ON COMPLETION PRESERVE ENABLE DO BEGIN
DECLARE month_var VARCHAR (200);
DECLARE year_var VARCHAR (200);
DECLARE month_len VARCHAR (200);
DECLARE sql_var VARCHAR (200) DEFAULT NULL;
DECLARE t_error INTEGER DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;
SELECT MONTH (DATE_SUB(CURDATE(),INTERVAL 66 DAY)) INTO month_var;
SELECT YEAR (DATE_SUB(CURDATE(),INTERVAL 66 DAY)) INTO year_var;
SELECT LENGTH(month_var) INTO month_len;
IF month_len = 1 THEN
SELECT
CONCAT(
'INSERT INTO park_record_',
year_var,
'0',
month_var,
' SELECT * FROM `park_record` WHERE CREATE_DATE <= DATE_ADD(CURDATE(), INTERVAL - 66 DAY)',
';'
) INTO sql_var;
ELSE
SELECT
CONCAT(
'INSERT INTO park_record_',
year_var,
month_var,
' SELECT * FROM `park_record` WHERE CREATE_DATE <= DATE_ADD(CURDATE(), INTERVAL - 66 DAY)',
';'
) INTO sql_var;
END IF;
SET @sql_var = sql_var;
PREPARE stmt FROM @sql_var;
START TRANSACTION;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
DELETE
FROM
park_record
WHERE CREATE_DATE <= DATE_ADD(CURDATE(), INTERVAL - 66 DAY) ;
IF t_error = 1 THEN
ROLLBACK;
ELSE
COMMIT;
END IF;
END$$