问题
I am developing a procedure in pl/sql that get a data set from a cursor executed in a function. For example:
Function:
f_process_data(id_process IN NUMBER, id_product IN NUMBER)
Returns :
v_result_cursor sys_refcursor;
But the problem is that in the search of the cursor I need to send at time more than one id_product. Something like this:
id_product: 1240 (sausages) id_product: 1260 (ham)
¿How can I send (or get) more than one product in the function?
I understood that it's possible with a loop, but how?
Something like this(?):
v_sausage := 1240;
v_ham := 1260;
LOOP
IF v_count = v_sausage OR v_count = v_ham THEN
v_result_cursor := f_process_data(1, p_id_product);
END IF;
FETCH v_result_cursor
INTO v_id, v_id_product;
EXIT WHEN v_result_cursor%NOTFOUND;
END LOOP;
I expected that v_id and v_id_product has been recorded in a collection including the results from the id_product 1240 and 1260.
But, if the business rules changes and is not only 2 products, how about 100 or 1000 products?
Note: the cursor from the function it's not possible to modify, the id_product must be sent one by one.
CURSOR cu_get_value_products IS
SELECT value FROM supplies WHERE id = p_id and id_product = p_id_product;
回答1:
With somewhat limited information the below is what I've come up with. I still have no clue what is happening with your STATIC Cursor which you mentioned in your question is defined in your f_process_data()
Function. Since I don't know the full code in this function I simply wrote my own and declared the CURSOR as SYS_REFCURSOR as that is what the function returns.
Please let me know if this works or if I'm missing some important information. I feel like I'm lacking crucial information in order to provide a useful solution to you.
The mock-up table I created called Products contains the following columns and data. See image.
DECLARE
/* Store Ref Cursor returned by f_process_data() Function */
v_result_cursor SYS_REFCURSOR;
/* Declare Record so we can store the data FETCHed from the Cursor */
rec_products products%ROWTYPE;
/* Declare a couple Product Variables for Proof of Concept */
v_sausage NUMBER;
v_ham NUMBER;
/* Store output */
n_id NUMBER;
v_id_product VARCHAR2(100);
/* Declare Type of TABLE NUMBER */
TYPE nt_type IS TABLE OF NUMBER;
/* Create Array/Table/Collection of type nt_type to store product ids */
nt_product_ids nt_type;
/* Returns a Ref Cursor based on the product_id used as Input to this function */
FUNCTION f_process_data(p_id_process IN NUMBER, p_id_product IN NUMBER)
RETURN SYS_REFCURSOR
AS
/* Declare Ref Cursor that will be Returned */
rc_result_cursor SYS_REFCURSOR;
BEGIN
/* Open Ref Cursor based on Product ID parameter */
OPEN rc_result_cursor FOR SELECT * FROM products WHERE item_id = p_id_product;
RETURN rc_result_cursor;
END f_process_data
;
BEGIN
/* Set Product Variables to IDs */
v_sausage := 2002;
v_ham := 2009;
/* Store product ids into a Number Table so we can Loop thru it */
nt_product_ids := nt_type (v_sausage,v_ham);
FOR r IN nt_product_ids.FIRST .. nt_product_ids.LAST
LOOP
/* Get Ref Cursor using SINGLE Product ID */
v_result_cursor := f_process_data(1, nt_product_ids(r));
LOOP
FETCH v_result_cursor INTO rec_products;
n_id := rec_products.item_id;
v_id_product := rec_products.item;
EXIT WHEN v_result_cursor%NOTFOUND;
dbms_output.put_line('Product_id: ' || n_id);
dbms_output.put_line('Product: ' || v_id_product);
END LOOP; /* Cursor Loop */
/* Close Cursor */
CLOSE v_result_cursor;
END LOOP; /* Product IDs Loop */
EXCEPTION WHEN OTHERS
THEN CLOSE v_result_cursor;
END;
回答2:
In order to send more than one product to be processed at a time, create a user defined Table Type in your database called ProductList. You can then send a Product list into a stored procedure as a parameter.
来源:https://stackoverflow.com/questions/56296617/how-to-get-data-from-a-pl-sql-function-if-a-parameter-have-more-than-one-value-i