我有以下存储过程:
DECLARE CURSORITO CURSOR FOR SELECT issues.id FROM issues
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
OPEN CURSORITO;
REPEAT
FETCH CURSORITO INTO c_id ;
IF NOT done THEN
-- TOTAL HOURS
SELECT SUM(hours) INTO c_Tot_Horas FROM time_entries WHERE issue_id=c_id;
INSERT INTO TOTAL (ID,TOT) VALUES c_id,c_Tot_Horas ;
END IF;
UNTIL done END REPEAT;
但是当“Total Hours Querys”没有数据时,进程退出repeat部分,将“Done”变量设置为true
我该如何才能很好地完成这个过程,也就是说,绕着所有光标走动?
答案 0 :(得分:1)
你不需要光标。
INSERT INTO total(ID, TOT)
SELECT issue_id, SUM(hours)
FROM time_entries t
/*optionally you can add*/
INNER JOIN issues i ON t.issue_id = i.id
/*if you need to filter by joining*/
GROUP BY issue_id
光标非常慢,不需要时可以不惜一切代价避免光标。