我有这个程序(不要太费心去弄清楚它的作用,针对名为“修改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 ;
除了行
之外,这很好用 UPDATE user_info SET notifications = 1 WHERE user_id = 1;
将无法正常工作,但在简单的纯SQL(phpmyadmin)中,此查询工作正常。有什么问题?
这个脚本的功能是什么?它可以帮助我向某些用户发布通知,当您在 group1 中发布某些内容时,该群组中 right101 的所有用户都必须像
一样得到通知 UPDATE user_info SET notifications = notifications + 1 WHERE user_id = var_user_id;
使用游标作为FOR LOOP
,就像我以前在PHP中使用的那样
这有什么问题?一个程序不能更新数据吗?! 希望我让自己明白了。
答案 0 :(得分:3)
不要放肆,但数据是否允许你超越
IF num_rows = 0
作为提示,如果您在SQL Management Studio中运行,则可以使用断点(如普通代码)调试sql。我建议在该行上设置一个断点,看看它是否真的被击中了。
答案 1 :(得分:1)
这些方面的内容怎么样?我通常在SQL Server中工作,所以如果某些语法关闭,我会道歉,但我提出了一些意见,所以我希望你能得到主旨。
/* 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 ;