Domanda

I am trying to get a ref_cursor to be assigned to a variable inside a for loop then returned at the end of a function. The loop in going through a local cursor if it gets more than 1 result.

I have noted where the error occurs in the code. I am not sure how to create a loop where i can get a ref_cursor for the current point in the loop, assign it to a variable and then return it to the function. Could someone someone help me figure out how to do that? Below is my i-th attempt at logic based of reading around the google searches.

The error is "PLS-00382: expression is of wrong type" and i know that i obviously am not assigning the correct variably type based on this error but the code below with the error is an illustration of what I want to do and what I need help accomplishing.

FUNCTION GET_PARCEL(p_lat in number, p_long in number) return sys_refcursor 
IS
  v_distance number(10) := 100000000;
  v_shortest_dist number(10) := v_distance;
  v_centroid SDO_GEOMETRY;
  v_rc_ref_cursor sys_refcursor;
  v_ref_geom SDO_GEOMETRY :=  mdsys.SDO_GEOMETRY(2001, 8311, NULL, SDO_ELEM_INFO_ARRAY(1, 1, 1), SDO_ORDINATE_ARRAY(120.3214, -10.7088));
  cursor query_cursor is select * from PARCEL_TABLE where code = 20134;
BEGIN
  for query_row in query_cursor loop
            v_centroid := SDO_GEOM.SDO_CENTROID(query_row.geometry, 0.05);
            IF (v_centroid is not null) then    
                v_distance := SDO_GEOM.SDO_DISTANCE(v_centroid, v_ref_geom, 0.05);
                IF v_distance < v_shortest_dist THEN
                    v_shortest_dist := v_distance;
                    v_rc_ref_cursor := query_row; -- Error on this line
                END IF;
            ELSE
                DBMS_OUTPUT.PUT_LINE('Centroid is not initialised for some reason.');
            END IF; 
  end loop;

  return v_rc_ref_cursor;
END;
È stato utile?

Soluzione 2

Pro tip is to remember you have data in your table and you can use it for logic. Turns out if I use the IDENTIFIER or ROWID of the row/s which i want then i can do a where clause into a ref cursor that looks up by IDENTIFIER or ROWID.

eg.

open v_rc_ref_cursor for select * from PARCEL_TABLE n where n.identifier = query_row.identifier;

super simple stuff :)

Altri suggerimenti

As far as I know, you cannot build up a cursor. Cursors are created and maintained by the database with connections to their source data, transaction and session context, and the like.

I would suggest you declare a type, instantiate it, build up the values in the type.

When you are done, create a cursor by selecting * from table (cast (variable as your_type)).

Munge the cursor into a ref_cursor and return that.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top