create funcion en mysql

Abril 19th, 2010 by admin

Para no olvidarme…

Necesitaba recorrer las actuaciones de las collas y solo tomar los 5 mejores resultados de las mismas.

CREATE FUNCTION spf_ultimas5( p_collaID INT) returns int

READS SQL DATA
NOT DETERMINISTIC

begin
DECLARE puntos_totales,puntos INT DEFAULT 0;
DECLARE done INT DEFAULT 0;
DECLARE rs CURSOR FOR
select punts_r
from actuacions
where collaID=p_collaID
order by punts_r  desc
limit 0,5 ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
OPEN rs;
repetir: LOOP
FETCH rs INTO puntos;

IF done=1 THEN
LEAVE repetir;
END IF;
SET  puntos_totales = puntos_totales  + puntos;
END LOOP  repetir;
CLOSE rs;
RETURN puntos_totales ;
end;

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.