Reputation: 405
I have this procedure (don't bother too much to figure it out what it does, aim for comments named "Modify 1,2,3,4" )
/* PROCEDURE 1 : Post notification */
DROP PROCEDURE IF EXISTS AddNotificationOnPosts;
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `AddNotificationOnPosts`(arg_from_user INT(11),arg_on_post_id INT(11),arg_in_group_id INT(11))
BEGIN
DECLARE num_rows INT DEFAULT NULL;
DECLARE insert_result INT DEFAULT NULL;
DECLARE user_id INT DEFAULT NULL;
DECLARE done INT DEFAULT 0;
DECLARE var_user_id INT DEFAULT NULL;
DECLARE c1 CURSOR FOR
SELECT user_id
FROM user_rights
WHERE user_rights.right = 101 AND user_rights.group_id = arg_in_group_id
ORDER BY user_id DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
IF(arg_from_user IS NULL OR arg_from_user = '')
THEN
SELECT "0" AS response;
ELSEIF(arg_on_post_id IS NULL OR arg_on_post_id = '')
THEN
SELECT "0" AS response;
ELSEIF(arg_in_group_id IS NULL OR arg_in_group_id = '')
THEN
SELECT "0" AS response;
ELSE
SELECT count(notification_id) FROM notifications_posts
WHERE
from_user = arg_from_user AND
on_post_id = arg_on_post_id AND
in_group_id = arg_in_group_id
INTO num_rows;
/* MODIFY 1*/
UPDATE user_info SET notifications = 1 WHERE user_id = 145;
END IF;
IF num_rows = 0
THEN
INSERT INTO notifications_posts(from_user,on_post_id,in_group_id) VALUES(arg_from_user,arg_on_post_id,arg_in_group_id);
SELECT ROW_COUNT() INTO insert_result;
/* MODIFY 2*/
UPDATE user_info SET notifications = 1 WHERE user_id = 1;
IF insert_result > 0
THEN
/* MODIFY 3*/
UPDATE user_info SET notifications = 1 WHERE user_id = 5;
/* Increment the notifications for every user*/
OPEN c1;
read_loop: LOOP
FETCH c1 INTO var_user_id;
IF done THEN
LEAVE read_loop;
ELSE
/* MODIFY 4*/
UPDATE user_info SET notifications = 1 WHERE user_id = 1;
END IF;
END LOOP;
CLOSE c1;
SELECT "1" AS response;
ELSE
SELECT "0" AS response;
END IF;
ELSE
SELECT "0" AS response;
END IF;
END $$
DELIMITER ;
This works just fine, except the lines
UPDATE user_info SET notifications = 1 WHERE user_id = 1;
won't work, but in simple plain SQL(phpmyadmin) this query is working fine. What is the problem?
What this script does? It helps me be able to post a notification to certain users, and when you post something in group1 all users that have right101 on that group must be notified like
UPDATE user_info SET notifications = notifications + 1 WHERE user_id = var_user_id;
using a cursor as a FOR LOOP
like i used to have in PHP
What is wrong with this? Can't a procedure update data?! Hope i made myself understandable.
Upvotes: 0
Views: 272
Reputation: 23113
How about something along these lines? I usually work in SQL Server, so I apologize if some of the syntax is off, but I threw in some comments, so I hope you get the gist.
/* PROCEDURE 1 : Post notification */
DROP PROCEDURE IF EXISTS AddNotificationOnPosts;
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `AddNotificationOnPosts`(arg_from_user INT(11), arg_on_post_id INT(11), arg_group_id INT(11))
BEGIN
-- sanity checks
IF(arg_from_user IS NULL OR arg_from_user = '')
THEN
RETURN 0;
ELSEIF(arg_on_post_id IS NULL OR arg_on_post_id <= 0)
THEN
RETURN 0;
ELSEIF(arg_in_group_id IS NULL OR arg_in_group_id <= 0)
THEN
RETURN 0;
END IF;
BEGIN TRAN;
-- insert if notification post does not exist
IF NOT EXISTS
(
SELECT *
FROM notification_posts
WHERE
from_user = arg_from_user AND
on_post_id = arg_on_post_id AND
in_group_id = arg_in_group_id
)
THEN
INSERT INTO notifications_posts
(
from_user,
on_post_id,
in_group_id
)
VALUES
(
arg_from_user,
arg_on_post_id,
arg_in_group_id
);
END IF;
-- update all users with 101 right
UPDATE ui
SET notifications = notifications + 1
FROM user_info ui
JOIN user_rights ur on ur.user_id = ui.user_id
WHERE ur.right = 101 and ur.group_id = arg_in_group_id
COMMIT;
END $$
DELIMITER ;
Upvotes: 1
Reputation: 473
Not to sound presumptuous but does the data allow you to get past the
IF num_rows = 0
As a tip though if you are running in SQL Management studio you can debug your sql with breakpoints like normal code. I suggest putting a breakpoint on that line and see if it actually gets hit at all.
Upvotes: 3