salix/db/routines/vn2008/procedures/splitAnthuriums2016.sql

87 lines
1.8 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`splitAnthuriums2016`()
BEGIN
DECLARE vStartingDate DATETIME DEFAULT '2016-10-18';
DECLARE vEndingDate DATETIME DEFAULT '2016-10-31 23:59:00';
DECLARE vMyTicket INT;
DECLARE vNewTicket INT;
-- DECLARE SILLA_PCA INT DEFAULT 44;
DECLARE done INT DEFAULT 0;
DECLARE rs CURSOR FOR
SELECT DISTINCT Id_Ticket
FROM Movimientos m
JOIN Tickets t using(Id_Ticket)
JOIN Articles a using(Id_Article)
JOIN Tipos tp using(tipo_id)
WHERE t.Fecha between vStartingDate and vEndingDate
AND warehouse_id = 1
AND
(
Id_Tipo = 'ANT'
OR
( Id_Article = 100
AND
Concepte LIKE 'ANT%')
);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN rs;
FETCH rs INTO vMyTicket;
WHILE NOT done DO
INSERT INTO Tickets(Id_Cliente
, warehouse_id
, Fecha
, Alias
, Id_Consigna
, Id_Trabajador
, Id_Ruta
, empresa_id
, Id_Agencia
, landing)
SELECT Id_Cliente
, SILLA_PCA as warehouse_id
, Fecha
, Alias
, Id_Consigna
, Id_Trabajador
, Id_Ruta
, empresa_id
, Id_Agencia
, landing
FROM Tickets
WHERE Id_Ticket = vMyTicket;
SELECT LAST_INSERT_ID() INTO vNewTicket;
-- SELECT vMyTicket, vNewTicket; SET done = 1;
UPDATE Movimientos m
JOIN Articles a using(Id_Article)
JOIN Tipos tp using(tipo_id)
SET Id_Ticket = vNewTicket
WHERE Id_Ticket = vMyTicket
AND (
Id_Tipo = 'ANT'
OR
( m.Id_Article = 100
AND
Concepte LIKE 'ANT%')
);
FETCH rs INTO vMyTicket;
END WHILE;
CLOSE rs;
END$$
DELIMITER ;