您好我是数据库和PL / SQL的新手,现在我正在学习如何使用游标。
我的问题是当我在Oracle SQL Developer中执行以下代码时,我得到一个无限循环。
set serveroutput on
DECLARE
CURSOR c1
IS
select tipoanimal, count(*), avg(precio)
from mi_cursos
group by tipoanimal;
xtotal_cursos number;
xtipo_tipoanimal mi_cursos.tipoanimal%type;
xcuenta_curso_animal number;
xprecio_medio_curso_animal number;
total_porcentaje number;
BEGIN
select count(*) into xtotal_cursos from mi_cursos;
OPEN c1;
LOOP
EXIT WHEN c1%NOTFOUND;
total_porcentaje:= xcuenta_curso_animal/xtotal_cursos*100;
dbms_output.put_line(rpad(xtipo_tipoanimal,10,' ')||
lpad(to_char(xcuenta_curso_animal,'999999'),10,' ')||
lpad(to_char(total_porcentaje,'99999.99'),10,' ')||
lpad(to_char(xprecio_medio_curso_animal, '999999'),10,' ') );
END LOOP;
CLOSE c1;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error -10: error no conocido');
dbms_output.put_line('Error Oracle ' || TO_CHAR(SQLCODE) || ' Mensaje: ' || SUBSTR(SQLERRM,1,200));
END;
这很奇怪,因为当光标完成时EXIT WHEN c1%NOTFOUND
喊叫让我离开循环。
有什么想法吗?
答案 0 :(得分:1)
您应该使用FETCH c1 INTO <variables>
子句。
也可以在没有open-fetch-close例程的情况下使用隐式游标。
顺便说一下,use可以为count(*), avg(precio)
函数提供列别名。