Return to Snippet

Revision: 30695
at August 19, 2010 17:31 by lucien144


Updated Code
DROP PROCEDURE IF EXISTS createQueenslandWards;
DELIMITER //
CREATE PROCEDURE createQueenslandWards()
	BEGIN
		DECLARE done INT DEFAULT 0;
		DECLARE wardName varchar(255);
		DECLARE Lang varchar(255);
		DECLARE Lat varchar(255);
		DECLARE cursorWard CURSOR FOR SELECT DISTINCT `ward` FROM `regions` WHERE `territory` = 'QLD';
		DECLARE cursorLangLat CURSOR FOR SELECT `centroid_y`, `centroid_x` FROM `regions` WHERE `ward` = wardName;
		DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

		OPEN cursorWard;

		REPEAT
			FETCH cursorWard INTO wardName;
			IF NOT done THEN
				OPEN cursorLanglat;
					block2: BEGIN
						DECLARE doneLangLat INT DEFAULT 0;
						DECLARE firstLang varchar(255) DEFAULT '';
						DECLARE firstLat varchar(255) DEFAULT '';
						DECLARE i int DEFAULT 1;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET doneLangLat = 1;
						
						REPEAT
							FETCH cursorLangLat INTO Lang, Lat;
							
							IF i == 1 THEN
								SET firstLang = Lang;
								SET firstLat = Lat;
							END IF;
							
							INSERT INTO `points` SET `coordinates` = GeomFromText(CONCAT('POINT(', Lang, ' ', Lat ,')')), `type` = wardName, to_text = CONCAT(Lang, ',', Lat), `description` = 'Queensland territory';
							SET i = i + 1;
						UNTIL doneLangLat END REPEAT;

						INSERT INTO `points` SET `coordinates` = GeomFromText(CONCAT('POINT(', firstLang, ' ', firstLat ,')')), `type` = wardName, to_text = CONCAT(firstLang, ',', firstLat), `description` = 'Queensland territory';

					END block2;
				CLOSE cursorLangLat;
			END IF;
		UNTIL done END REPEAT; 

		CLOSE cursorWard;

	END//
DELIMITER ;
CALL createQueenslandWards();

Revision: 30694
at August 19, 2010 17:02 by lucien144


Updated Code
DROP PROCEDURE IF EXISTS createQueenslandWards;
DELIMITER //
CREATE PROCEDURE createQueenslandWards()
	BEGIN
		DECLARE done INT DEFAULT 0;
		DECLARE wardName varchar(255);
		DECLARE Lang varchar(255);
		DECLARE Lat varchar(255);
		DECLARE cursorWard CURSOR FOR SELECT DISTINCT `ward` FROM `regions` WHERE `territory` = 'QLD';
		DECLARE cursorLangLat CURSOR FOR SELECT `centroid_y`, `centroid_x` FROM `regions` WHERE `ward` = wardName;
		DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

		OPEN cursorWard;

		REPEAT
			FETCH cursorWard INTO wardName;
			IF NOT done THEN
				OPEN cursorLanglat;
					block2: BEGIN
						DECLARE doneLangLat INT DEFAULT 0;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET doneLangLat = 1;
						
						REPEAT
							FETCH cursorLangLat INTO Lang, Lat;
							INSERT INTO `points` SET `coordinates` = GeomFromText(CONCAT('POINT(', Lang, ' ', Lat ,')')), `type` = wardName, to_text = CONCAT(Lang, ',', Lat), `description` = 'Queensland territory';
						UNTIL doneLangLat END REPEAT;

					END block2;
				CLOSE cursorLangLat;
			END IF;
		UNTIL done END REPEAT; 

		CLOSE cursorWard;

	END//
DELIMITER ;
CALL createQueenslandWards();

Revision: 30693
at August 19, 2010 16:59 by lucien144


Initial Code
SELECT DISTINCT `ward` FROM `regions` WHERE `territory` = 'QLD';
DROP PROCEDURE IF EXISTS createQueenslandWards;
DELIMITER //
CREATE PROCEDURE createQueenslandWards()
	BEGIN
		DECLARE done INT DEFAULT 0;
		DECLARE wardName varchar(255);
		DECLARE Lang varchar(255);
		DECLARE Lat varchar(255);
		DECLARE cursorWard CURSOR FOR SELECT DISTINCT `ward` FROM `regions` WHERE `territory` = 'QLD';
		DECLARE cursorLangLat CURSOR FOR SELECT `centroid_y`, `centroid_x` FROM `regions` WHERE `ward` = wardName;
		DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

		OPEN cursorWard;

		REPEAT
			FETCH cursorWard INTO wardName;
			IF NOT done THEN
				OPEN cursorLanglat;
					block2: BEGIN
						DECLARE doneLangLat INT DEFAULT 0;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET doneLangLat = 1;
						
						REPEAT
							FETCH cursorLangLat INTO Lang, Lat;
							INSERT INTO `points` SET `coordinates` = GeomFromText(CONCAT('POINT(', Lang, ' ', Lat ,')')), `type` = wardName, to_text = CONCAT(Lang, ',', Lat), `description` = 'Queensland territory';
						UNTIL doneLangLat END REPEAT;

					END block2;
				CLOSE cursorLangLat;
			END IF;
		UNTIL done END REPEAT; 

		CLOSE cursorWard;

	END//
DELIMITER ;
CALL createQueenslandWards();

Initial URL

                                

Initial Description
This snippet just showing how to make nested loops with cursors in MySQL.

Initial Title
Nested loop with cursor in procedure

Initial Tags

                                

Initial Language
MySQL