salix/db/routines/bs/procedures/clean.sql

35 lines
1014 B
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`clean`()
BEGIN
DECLARE vFourYearsAgo DATE DEFAULT util.VN_CURDATE() - INTERVAL 1 YEAR;
DECLARE vOneYearAgo DATE DEFAULT util.VN_CURDATE() - INTERVAL 2 YEAR;
DECLARE vTwoYearAgo DATE DEFAULT util.VN_CURDATE() - INTERVAL 3 YEAR;
DECLARE vThreeYearAgo DATE DEFAULT util.VN_CURDATE() - INTERVAL 4 YEAR;
DECLARE vEighteenMonths DATE DEFAULT util.VN_CURDATE() - INTERVAL 18 MONTH;
DELETE FROM clientNewBorn
WHERE lastShipped < vOneYearAgo;
DELETE FROM ventas
WHERE fecha < vFourYearsAgo;
DELETE FROM payMethodClient
WHERE dated < vOneYearAgo;
DELETE FROM payMethodClientEvolution
WHERE dated < vFourYearsAgo;
DELETE FROM salesByclientSalesPerson
WHERE dated < vFourYearsAgo;
DELETE FROM m3
WHERE fecha < vTwoYearAgo;
DELETE FROM salesByItemTypeDay
WHERE dated < vThreeYearAgo;
DELETE FROM waste
WHERE MAKEDATE(`year`, 1) + INTERVAL (`week` - 1) WEEK + INTERVAL 1 DAY < vEighteenMonths;
END$$
DELIMITER ;