28 lines
749 B
MySQL
28 lines
749 B
MySQL
|
DELIMITER $$
|
||
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`worker_getHierarchy`(vUserFk INT)
|
||
|
BEGIN
|
||
|
/**
|
||
|
* Retorna una tabla temporal con los trabajadores que tiene
|
||
|
* el usuario por debajo en la jerarquía de la empresa.
|
||
|
*
|
||
|
* @param vUserFk Id de usuario.
|
||
|
* @return tmp.workerHierarchyList
|
||
|
*/
|
||
|
CREATE OR REPLACE TEMPORARY TABLE tmp.workerHierarchyList
|
||
|
(PRIMARY KEY (workerFk))
|
||
|
ENGINE = MEMORY
|
||
|
WITH RECURSIVE workerHierarchy AS (
|
||
|
SELECT id workerFk, bossFk, 0 depth
|
||
|
FROM vn.worker
|
||
|
WHERE id = vUserFk
|
||
|
UNION ALL
|
||
|
SELECT w.id, w.bossFk, wh.depth + 1
|
||
|
FROM vn.worker w
|
||
|
JOIN workerHierarchy wh ON w.bossFk = wh.workerFk
|
||
|
)
|
||
|
SELECT *
|
||
|
FROM workerHierarchy
|
||
|
ORDER BY depth, workerFk;
|
||
|
END$$
|
||
|
DELIMITER ;
|