Tengo el siguiente procedimiento
el cual me funciona bien, solo quiero compartir mi experiencia con uds y contarles mi duda
DROP TRIGGER IF EXISTS trg_ai_op_event_list;
delimiter //
CREATE TRIGGER trg_ai_op_event_list
AFTER INSERT ON op_event_list
for each row
BEGIN
-- agregar si el usuario recibe el tipo de notificaion
DECLARE done INT DEFAULT 0;
DECLARE f_id INT;
DECLARE friends CURSOR FOR SELECT f.friend_id FROM friend f WHERE f.user_id = NEW.user_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN friends;
read_loop: LOOP
FETCH friends INTO f_id; -- LA DIFERENCIA
IF done = 1 THEN
LEAVE read_loop;
END IF;
-- insertar una notificaion a todos los amigos del usuario disparador del evento
insert into op_event_user_notifications values (null, f_id ,NEW.id, null, false);
END LOOP;
END
//
Antes no me funcionaba, se ejecutaba mas veces de la que debia y en f_id me insertaba null
DROP TRIGGER IF EXISTS trg_ai_op_event_list;
delimiter //
CREATE TRIGGER trg_ai_op_event_list
AFTER INSERT ON op_event_list
for each row
BEGIN
-- agregar si el usuario recibe el tipo de notificaion
DECLARE done INT DEFAULT 0;
DECLARE f_id INT;
DECLARE friends CURSOR FOR SELECT f.friend_id FROM friend f WHERE f.user_id = NEW.user_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN friends;
read_loop: LOOP
IF done = 1 THEN
LEAVE read_loop;
END IF;
-- insertar una notificaion a todos los amigos del usuario disparador del evento
FETCH friends INTO f_id; -- LA DIFERENCIA
insert into op_event_user_notifications values (null, f_id ,NEW.id, null, false);
END LOOP;
END
//
Explicando lo que hago:
Este evento se ejecuta cada vez que inserto un registro en la tabla 'op_event_list'
y lo que hace es insertar en la tabla de notificaciones 'op_event_user_notirications'
un registro para los usuarios amigos del usuario que
ejecuto una tarea y registro un evento determinado.
Lo cierto es que cambie de lugar donde hago el 'FETCH'
realmente no entiendo en que cambian las cosas pero :
- en el primer caso funciona
- en el segundo caso no funciona