Reputation: 23
I am trying to write a code which can find all possible routes from a graph table. The code should be worked by using a single input value. E.g. I want to find all possible routes from point A to Point B.
I think that a procedure can maybe solve the problem, but I get the error message.
The graph tabel (a overview of all routes) is
P_FROM P_TO DISTANCE
A B 4
A C 7
B C 10
C D 15
B D 17
A D 23
B E 22
C E 29
and the procedure is
CREATE OR REPLACE PROCEDURE p_find_all_routes (
p_start IN VARCHAR2 DEFAULT '%',
p_end IN VARCHAR2 DEFAULT '%',
p_via IN VARCHAR2 DEFAULT '%')
AS
BEGIN
-- =======================================================================
-- Author: Coilin P. Boylan Jeritslev (CTBJ)
-- Description: Find all possible routes between two different points
-- "p_start" and "p_end" via the choosen point "p_via" in a graph-tabel.
-- =======================================================================
WITH multiroutes (p_from, p_to, full_route, total_distance)
AS (SELECT p_from,
p_to,
p_from || '->' || p_to full_route,
distance total_distance
FROM graph
WHERE p_from LIKE p_start
UNION ALL
SELECT M.p_from,
n.p_to,
M.full_route || '->' || n.p_to full_route,
M.total_distance + n.distance total_distance
FROM multiroutes M JOIN graph n ON M.p_to = n.p_from
WHERE n.p_to <> ALL (M.full_route))
SELECT *
FROM multiroutes
WHERE p_to LIKE p_end
AND ( full_route LIKE ('%->' || p_via || '%')
OR full_route LIKE ('%' || p_via || '->%'))
ORDER BY p_from, p_to, total_distance ASC;
END;
/
When I'm executing the procedure with the following inputs:
EXEC p_find_all_routes('A','E','%')
I expect, the procedure is compiled and I get the result
P_FROM P_TO FULL_ROUTE TOTAL_DISTANCE
A E A->B->E 26
A E A->C->E 36
A E A->B->C->E 43
But I get the PLS-00428 error message. Something about INTO.
I don't want to insert output values into a tabel. I want just to see the output values. How can I do it?
Upvotes: 2
Views: 68
Reputation: 13509
You cannot return the result set in Oracle by using only Query. You need to use Ref cursor for the same. You can try below code -
CREATE OR REPLACE PROCEDURE p_find_all_routes (
p_start IN VARCHAR2 DEFAULT '%',
p_end IN VARCHAR2 DEFAULT '%',
p_via IN VARCHAR2 DEFAULT '%',
multiroutes OUT SYS_REFCURSOR)
AS
BEGIN
-- =======================================================================
-- Author: Coilin P. Boylan Jeritslev (CTBJ)
-- Description: Find all possible routes between two different points
-- "p_start" and "p_end" via the choosen point "p_via" in a graph-tabel.
-- =======================================================================
OPEN multiroutes FOR
WITH multiroutes (p_from, p_to, full_route, total_distance)
AS (SELECT p_from,
p_to,
p_from || '->' || p_to full_route,
distance total_distance
FROM graph
WHERE p_from LIKE p_start
UNION ALL
SELECT M.p_from,
n.p_to,
M.full_route || '->' || n.p_to full_route,
M.total_distance + n.distance total_distance
FROM multiroutes M JOIN graph n ON M.p_to = n.p_from
WHERE n.p_to <> ALL (M.full_route))
SELECT *
FROM multiroutes
WHERE p_to LIKE p_end
AND ( full_route LIKE ('%->' || p_via || '%')
OR full_route LIKE ('%' || p_via || '->%'))
ORDER BY p_from, p_to, total_distance ASC;
END;
/
You can then call this procedure later by declaring Ref cursor variable.
DECLARE
Result SYS_REFCURSOR;
BEGIN
p_find_all_routes('A','E','%', Result);
END;
Upvotes: 2