Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Gibt es eine Möglichkeit, mehrere Zeilen mithilfe von Parametern auszuwählen?

Sie können das Resultset nicht in Oracle zurückgeben, indem Sie nur Query verwenden. Sie müssen dafür den Ref-Cursor verwenden. Sie können den folgenden Code ausprobieren -

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;
/

Sie können diese Prozedur später aufrufen, indem Sie die Ref-Cursor-Variable deklarieren.

DECLARE
    Result SYS_REFCURSOR;
BEGIN
    p_find_all_routes('A','E','%', Result);
END;