如何在同一例程中使用两个游标?如果我删除第二个游标声明和获取循环everthing工作正常。该例程用于在我的webapp中添加朋友。它需要当前用户的id和我们想要添加为朋友的朋友的电子邮件,然后它检查电子邮件是否具有相应的用户ID,如果不存在朋友关系,则会创建一个。除此之外的任何其他常规解决方案都会很棒。
DROP PROCEDURE IF EXISTS addNewFriend;
DELIMITER //
CREATE PROCEDURE addNewFriend(IN inUserId INT UNSIGNED, IN inFriendEmail VARCHAR(80))
BEGIN
DECLARE tempFriendId INT UNSIGNED DEFAULT 0;
DECLARE tempId INT UNSIGNED DEFAULT 0;
DECLARE done INT DEFAULT 0;
DECLARE cur CURSOR FOR
SELECT id FROM users WHERE email = inFriendEmail;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
REPEAT
FETCH cur INTO tempFriendId;
UNTIL done = 1 END REPEAT;
CLOSE cur;
DECLARE cur CURSOR FOR
SELECT user_id FROM users_friends WHERE user_id = tempFriendId OR friend_id = tempFriendId;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
REPEAT
FETCH cur INTO tempId;
UNTIL done = 1 END REPEAT;
CLOSE cur;
IF tempFriendId != 0 AND tempId != 0 THEN
INSERT INTO users_friends (user_id, friend_id) VALUES(inUserId, tempFriendId);
END IF;
SELECT tempFriendId as friendId;
END //
DELIMITER ;
答案 0 :(得分:16)
以下是如何在同一例程中使用两个游标的简单示例:
DELIMITER $$
CREATE PROCEDURE `books_routine`()
BEGIN
DECLARE rowCountDescription INT DEFAULT 0;
DECLARE rowCountTitle INT DEFAULT 0;
DECLARE updateDescription CURSOR FOR
SELECT id FROM books WHERE description IS NULL OR CHAR_LENGTH(description) < 10;
DECLARE updateTitle CURSOR FOR
SELECT id FROM books WHERE title IS NULL OR CHAR_LENGTH(title) <= 10;
OPEN updateDescription;
BEGIN
DECLARE exit_flag INT DEFAULT 0;
DECLARE book_id INT(10);
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET exit_flag = 1;
updateDescriptionLoop: LOOP
FETCH updateDescription INTO book_id;
IF exit_flag THEN LEAVE updateDescriptionLoop;
END IF;
UPDATE books SET description = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.' WHERE books.id = book_id;
SET rowCountDescription = rowCountDescription + 1;
END LOOP;
END;
CLOSE updateDescription;
OPEN updateTitle;
BEGIN
DECLARE exit_flag INT DEFAULT 0;
DECLARE book_id INT(10);
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET exit_flag = 1;
updateTitleLoop: LOOP
FETCH updateTitle INTO book_id;
IF exit_flag THEN LEAVE updateTitleLoop;
END IF;
UPDATE books SET title = 'Lorem ipsum dolor sit amet' WHERE books.id = book_id;
SET rowCountTitle = rowCountTitle + 1;
END LOOP;
END;
CLOSE updateTitle;
SELECT 'number of titles updated =', rowCountTitle, 'number of descriptions updated =', rowCountDescription;
END
答案 1 :(得分:4)
我知道你找到了一个更好的解决方案,但我相信你原来问题的答案是你需要SET Done = 0;在两个游标之间,否则第二个游标只会在退出循环之前获取一个记录,因为前一个处理程序的Done = 1。
答案 2 :(得分:2)
我终于写了一个不同的功能,它做了同样的事情:
DROP PROCEDURE IF EXISTS addNewFriend;
DELIMITER //
CREATE PROCEDURE addNewFriend(IN inUserId INT UNSIGNED, IN inFriendEmail VARCHAR(80))
BEGIN
SET @tempFriendId = (SELECT id FROM users WHERE email = inFriendEmail);
SET @tempUsersFriendsUserId = (SELECT user_id FROM users_friends WHERE user_id = inUserId AND friend_id = @tempFriendId);
IF @tempFriendId IS NOT NULL AND @tempUsersFriendsUserId IS NULL THEN
INSERT INTO users_friends (user_id, friend_id) VALUES(inUserId, @tempFriendId);
END IF;
SELECT @tempFriendId as friendId;
END //
DELIMITER ;
我希望这是一个更好的解决方案,无论如何都可行。谢谢你告诉我在没有必要的时候不要使用游标。
答案 3 :(得分:1)
您可以在WHERE子句中使用EXISTS子句,而不是使用游标来检查是否存在记录:
INSERT INTO users_friends
(user_id, friend_id)
VALUES
(inUserId, tempFriendId)
WHERE EXISTS(SELECT NULL
FROM users
WHERE email = inFriendEmail)
AND NOT EXISTS(SELECT NULL
FROM users_friends
WHERE user_id = tempFriendId
AND friend_id = tempFriendId);
我在阅读了保罗关于第二个查询的评论之后做了改动,并颠倒了逻辑,因此插入不会添加重复项。理想情况下,这应作为主键处理,作为复合键(包括两列或更多列),这将停止检入代码的需要。
答案 4 :(得分:0)
SQL是一种基于集合的语言,一般来说,游标很糟糕,有些情况下它们很有用,但它们很少见。你在这里使用游标是完全不合适的。
你在第二个光标中的逻辑也有缺陷,因为它会选择任何包含朋友的记录,而不仅仅是所需的友谊。
如果你想修复它,你可以尝试给第二个光标一个不同的名称,但最好重新开始。
在users_friends上设置复合PK或唯一约束,然后您不必担心检查关系,然后尝试这样的事情。
INSERT INTO users_friends
SELECT
@inUserId,
users.user_id
FROM
users
WHERE
email = @inFriendEmail