0

我有以下存储过程:

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

我应该怎么做才能很好地完成这个过程,即围绕所有光标走动?

4

1 回答 1

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

光标非常慢,当你不需要它们时,不惜一切代价避免它们。

于 2013-04-09T11:12:47.947 回答