47 lines
1.2 KiB
SQL
47 lines
1.2 KiB
SQL
DELIMITER $$
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`subordinate`(IN `vWorker` INT, IN `vBaseCase` BOOLEAN)
|
|
NO SQL
|
|
BEGIN
|
|
-- deprecated usar vn.worker_GetHierarch
|
|
DECLARE vAuxWorker INT;
|
|
|
|
IF vBaseCase THEN
|
|
SET @@max_sp_recursion_depth = 5 ;
|
|
DROP TEMPORARY TABLE IF EXISTS subordinate;
|
|
CREATE TEMPORARY TABLE subordinate
|
|
(INDEX (Id_Trabajador))
|
|
ENGINE = MEMORY
|
|
SELECT vWorker Id_Trabajador, TRUE visited, 0 boss;
|
|
END IF;
|
|
|
|
INSERT INTO subordinate
|
|
SELECT Id_Trabajador, FALSE, vWorker
|
|
FROM Trabajadores WHERE boss = vWorker;
|
|
|
|
IF (SELECT COUNT(*) FROM subordinate WHERE NOT visited AND vWorker = boss LIMIT 1) > 0 THEN
|
|
SELECT Id_Trabajador INTO vAuxWorker
|
|
FROM subordinate
|
|
WHERE NOT visited AND vWorker = boss LIMIT 1;
|
|
|
|
REPEAT
|
|
CALL subordinate(vAuxWorker, FALSE);
|
|
UPDATE subordinate SET visited = TRUE WHERE Id_Trabajador = vAuxWorker;
|
|
SET vAuxWorker = 0;
|
|
|
|
SELECT Id_Trabajador INTO vAuxWorker
|
|
FROM subordinate
|
|
WHERE NOT visited
|
|
AND vWorker = boss
|
|
LIMIT 1;
|
|
|
|
UNTIL vAuxWorker = 0
|
|
END REPEAT;
|
|
END IF;
|
|
|
|
IF vWorker = 2 THEN
|
|
INSERT INTO subordinate
|
|
SET Id_Trabajador = vWorker, visited = TRUE, boss = vWorker;
|
|
END IF;
|
|
END$$
|
|
DELIMITER ;
|