CREATE
DEFINER=`root`@`localhost`
TRIGGER `meemor`.`addnotification`
AFTER UPDATE ON `meemor`.`posts`
FOR EACH ROW
BEGIN
DECLARE _user_id BIGINT;
DECLARE no_more_rows BOOLEAN;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;
DECLARE new_post INT;
set new_post = NEW.post_id;
DECLARE notifition_group CURSOR FOR select user_id FROM posts WHERE post_id = new_post;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE;
OPEN notifition_group;
the_loop: LOOP
FETCH notifition_group
INTO _user_id;
IF no_more_rows THEN CLOSE notifition_group; LEAVE the_loop; END IF;
SET loop_cntr = loop_cntr + 1;
INSERT INTO notifications (user_id)
VALUES(_user_id);
END LOOP the_loop;
END$$
Mark as Answer the posts that helped you.
Marked as answer by badzeugene on Apr 04, 2012 05:50 PM
bugwee
Member
354 Points
72 Posts
Re: how to correct this mysql triggers?
Apr 03, 2012 03:22 AM|LINK
try,
CREATE DEFINER=`root`@`localhost` TRIGGER `meemor`.`addnotification` AFTER UPDATE ON `meemor`.`posts` FOR EACH ROW BEGIN DECLARE _user_id BIGINT; DECLARE no_more_rows BOOLEAN; DECLARE loop_cntr INT DEFAULT 0; DECLARE num_rows INT DEFAULT 0; DECLARE new_post INT; set new_post = NEW.post_id; DECLARE notifition_group CURSOR FOR select user_id FROM posts WHERE post_id = new_post; DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE; OPEN notifition_group; the_loop: LOOP FETCH notifition_group INTO _user_id; IF no_more_rows THEN CLOSE notifition_group; LEAVE the_loop; END IF; SET loop_cntr = loop_cntr + 1; INSERT INTO notifications (user_id) VALUES(_user_id); END LOOP the_loop; END$$