从来自函数的游标中获取数据

Fetch data from a Cursor that comes from a Function

我创建了一个 returns 一个 SYS_REFCURSOR、

的函数
FUNCTION read_addresses (person_id NUMBER)
        RETURN SYS_REFCURSOR
    IS
        my_addresses   SYS_REFCURSOR;
    BEGIN
        OPEN my_addresses FOR
            SELECT commuter_name,
                   address_line,
                   city_name,
                   lat_lon,
                   my_dist,
              FROM carpool.addresses  addr
                   INNER JOIN CARPOOL.COMMUTERS comm
                       ON addr.COMMUTER_ID = comm.COMMUTER_ID
                   INNER JOIN CARPOOL.CITIES city
                       ON addr.city_id = city.CITY_ID
                   INNER JOIN carpool.coordinates coord
                       ON coord.COORD_ID = addr.COORD_ID
             WHERE comm.commuter_id = person_id AND addr.is_active = 1;

        RETURN my_addresses;
    END read_addresses;

而且我想做一个匿名块来测试这个功能。我目前拥有的:

DECLARE
    my_cursor    SYS_REFCURSOR;
    my_name      VARCHAR2 (100);
    my_address   VARCHAR2 (100);
    my_city      VARCHAR2 (100);
    my_latlon    VARCHAR2 (100);
    my_dist      NUMBER;
BEGIN
    my_cursor := carpool.irud.read_addresses (12);

    OPEN my_cursor;

    LOOP
        FETCH my_cursor
            INTO my_name,
                 my_address,
                 my_city,
                 my_latlon,
                 my_dist;

        EXIT WHEN my_cursor%NOTFOUND;
        DBMS_OUTPUT.put_line (my_name);
    END LOOP;

    CLOSE my_cursor;
END;

产生以下错误: 第 11 行第 5 列的表达式类型错误(我认为那是 OPEN my_cursor 行。

我需要做什么来测试这个功能并查看光标的内容?

如果您能帮助我解决这个问题,我将不胜感激。

这里的问题是调用函数后游标已经打开了。不用写OPEN my_cursor。删除此行可解决问题。