使用mysql使用存储过程获取和更新数据

时间:2018-03-08 10:29:53

标签: mysql stored-procedures

我有一个存储过程,我想首先从表中获取所有数据并使用相同的存储过程更新数据。我尝试过使用变量但不能这样做。

请为此建议一些解决方案。

create procedure Std(in taskDone int)
select i.id from student join teacher
group by i.id;
update student set status = taskDone

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以这样做:

DELIMITER //
CREATE PROCEDURE checkUser(IN in_email VARCHAR(80), IN in_password VARCHAR(50))
BEGIN
    DECLARE tempId INT DEFAULT 0;
    DECLARE tempName VARCHAR(50) DEFAULT NULL;
    DECLARE done INT DEFAULT 0;

    DECLARE cur CURSOR FOR 
        SELECT id, name FROM users WHERE email = in_email AND password = in_password;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

    OPEN cur;

    REPEAT
        FETCH cur INTO tempId, tempName;
        UPDATE users SET online = 1 WHERE id = tempId;
    UNTIL done  = 1 END REPEAT;
    CLOSE cur;

    SELECT tempName;
END //
DELIMITER ;