All pastes #742114 Raw Edit

AzMoo

public text v1 · immutable
#742114 ·published 2007-10-19 07:10 UTC
rendered paste body
DELIMITER $$

DROP FUNCTION IF EXISTS `helpdesk`.`ISCHILDOF`$$

CREATE DEFINER=`matt`@`%` FUNCTION `ISCHILDOF`(cID INT, pID INT) RETURNS tinyint(1)
    READS SQL DATA
BEGIN
	DECLARE currentChild INT;
	DECLARE res BOOL;
	DECLARE cur CURSOR FOR SELECT parentId FROM categories WHERE categoryId = currentChild;

	IF (pID = 0) THEN
		RETURN true;
	END IF;
	SET res = false;
			SET currentChild = cID;
			WHILE (currentChild != 0) DO
				IF (currentChild = pID) THEN 
					SET res = true;
					SET currentChild = 0;
				ELSE
					OPEN cur;
						FETCH cur INTO currentChild;
					CLOSE cur;
				END IF;
		
			END WHILE;
			
			RETURN res;
    END$$

DELIMITER ;

=================================================

mysql> SELECT * FROM categories;
+------------+----------+------------------------+
| categoryId | parentId | description            |
+------------+----------+------------------------+
|          1 |        0 | DealerPro              |
|        123 |        1 | sdagsdgaga             |
|        124 |      122 | sdagdgag               |
|        125 |      124 | sdaggdasga             |
|        122 |        1 | sdg                    |
|        120 |      119 | New Categorygdsagdsaga |
|         98 |        0 | EasyServe              |
|         97 |        0 | EasySale               |
+------------+----------+------------------------+
8 rows in set (0.00 sec)

=================================================

DELETE FROM categories WHERE ISCHILDOF(categoryId, 1) OR categoryId = 1