I will gratefully if someone can help me with this function in oracle pl/sql. I trying to return a cursor with ids from one table, but when try to execute the Anonyme block calling the function I receive an exception with code ORA-06504. The functión returns a cursor when select get registers from query, but when the query not get registers I need to raise the personalize exception. Thanks a lot for your help...
create or replace function FN_VALIDA_DISPONIBILIDAD(f_desde in date , f_hasta in date)return SYS_REFCURSOR
is
v_prop propiedad%rowtype;
c_prop_disponibles sys_refcursor;
fecha_no_disponible exception;
manso_error exception;
aux number:=0;
begin
OPEN c_prop_disponibles FOR
select p4.id_propiedad
from propiedad p4
where p4.id_propiedad NOT IN
(
SELECT p.ID_PROPIEDAD
FROM propiedad p
LEFT JOIN reserva r
ON r.id_propiedad = p.ID_PROPIEDAD
WHERE r.ID_RESERVA IS NULL
AND (
(f_desde > r.FECHA_TERMINO_RESER)
OR
(f_desde < r.FECHA_INICIO_RESER
AND
f_hasta< r.FECHA_INICIO_RESER )
)
OR
(r.FECHA_INICIO_RESER BETWEEN f_desde AND f_hasta)
OR
r.FECHA_TERMINO_RESER BETWEEN f_desde AND f_hasta
)
ORDER BY ID_PROPIEDAD;
LOOP
FETCH c_prop_disponibles INTO v_prop;
EXIT WHEN c_prop_disponibles%NOTFOUND;
if c_prop_disponibles%notfound then
DBMS_OUTPUT.PUT_LINE('No hay');
aux:=0;
elsif c_prop_disponibles%found then
DBMS_OUTPUT.PUT_LINE('hay');
--return c_prop_disponibles;
aux:=1;
else
raise manso_error;
end if;
END LOOP;
begin
if aux = 1 then
return c_prop_disponibles;
else
raise fecha_no_disponible;
end if;
end;
exception
when fecha_no_disponible then
raise_application_error(-20006, 'No hay disponibilidad!!! '||sqlcode||' - '||'ERROR '||SQLERRM);
when manso_error then
raise_application_error(-20017,'Ocurrio un manso error!!!'||sqlcode||' - '||'ERROR '||SQLERRM);
when others then
raise_application_error(-20011,'Ocurrio un error!!!'||sqlcode||' - '||'ERROR '||SQLERRM);
end FN_VALIDA_DISPONIBILIDAD;