我尝试使用此查询更新表格:
DELIMITER $$
CREATE PROCEDURE updateDataSetHasChildren()
BEGIN
DECLARE data_set_id INT;
DECLARE done INT DEFAULT FALSE;
DECLARE result INT DEFAULT FALSE;
DECLARE data_set_cursor CURSOR FOR
SELECT id_data_set FROM data_set;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN data_set_cursor;
myloop: LOOP
-- Perform the first fetch.
FETCH data_set_cursor into data_set_id;
IF done THEN
LEAVE myloop;
END IF;
IF (SELECT COUNT(*) FROM data_sub_set WHERE id_data_set = data_set_id)>0 THEN
UPDATE data_set
SET has_children = TRUE
WHERE id_data_set = data_set_id;
ELSE
UPDATE data_set
SET has_children = FALSE
WHERE id_data_set = data_set_id;
END IF;
FETCH data_set_cursor into data_set_id;
END LOOP myloop;
CLOSE data_set_cursor;
END$$
DELIMITER ;
问题是,当我运行它时,它填充表的列1 0 1 0 1 0 ...当我检查值不正确时,我在这里缺少什么?
此致
答案 0 :(得分:1)
删除第二个
FETCH data_set_cursor into data_set_id;
在myloop
的末尾。现在它正在按步骤工作:
获取执行两次,您实际上每隔一行处理一次。