56 lines
1.2 KiB
SQL
56 lines
1.2 KiB
SQL
DROP procedure IF EXISTS `vn`.`zoneNestCountry`;
|
|
|
|
DELIMITER $$
|
|
CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`zoneNestCountry`()
|
|
BEGIN
|
|
|
|
DECLARE vDone BOOL;
|
|
DECLARE vParent INT DEFAULT 1;
|
|
DECLARE vGeoFk INT;
|
|
DECLARE vChildFk INT;
|
|
DECLARE vChildName VARCHAR(100);
|
|
DECLARE countryCur CURSOR FOR
|
|
SELECT 1, c.id, c.`country`
|
|
FROM vn.country c
|
|
JOIN tmp.country tc ON tc.id = c.id
|
|
ORDER BY c.`country`;
|
|
|
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
|
|
|
-- Remove existing countries
|
|
DELETE zg FROM vn.country c
|
|
JOIN tmp.country tc ON tc.id = c.id
|
|
JOIN tmp.zoneGeo zg ON zg.id = c.geoFk;
|
|
|
|
-- Reset country geoFk
|
|
UPDATE vn.country c
|
|
JOIN tmp.country tc ON tc.id = c.id
|
|
SET c.geoFk = NULL
|
|
WHERE c.geoFk IS NOT NULL;
|
|
|
|
-- > Country cursor start
|
|
OPEN countryCur;
|
|
|
|
countryLoop: LOOP
|
|
SET vDone = FALSE;
|
|
|
|
FETCH countryCur INTO vParent, vChildFk, vChildName;
|
|
|
|
IF vDone THEN
|
|
LEAVE countryLoop;
|
|
END IF;
|
|
|
|
CALL nst.nodeAdd('tmp', 'zoneGeo', vParent, vChildName);
|
|
END LOOP;
|
|
CLOSE countryCur;
|
|
-- < Country cursor end
|
|
|
|
UPDATE country c
|
|
JOIN tmp.zoneGeo z ON z.name = c.country
|
|
SET c.geoFk = z.id
|
|
WHERE c.geoFk IS NULL;
|
|
END$$
|
|
|
|
DELIMITER ;
|
|
|