我有一个这样的包:
CREATE OR REPLACE PACKAGE product_package AS
TYPE t_ref_cursor to IS REF CURSOR;
FUNCTION get_products_ref_cursor RETURN t_ref_cursor;
END product_package;
CREATE OR REPLACE PACKAGE BODY product_package AS
FUNCTION get_products_ref_cursor is RETURN t_ref_cursor IS
products_ref_cursor t_ref_cursor;
BEGIN
OPEN products_ref_cursor FOR
SELECT product_id, name, price FROM Products;
RETURN products_ref_cursor;
END get_products_ref_cursor;
END product_package;
我的问题是,我如何使用函数get_products_ref_cursor (ref cursor)来获取产品列表?
declare
type rec is record(produc_id number, name varchar2(x), price number);
type l_rec is table of rec;
v_l_rec l_rec;
v_cursor product_package.t_ref_cursor;
begin
v_cursor := product_package.get_products_ref_cursor;
fetch v_cursor bulk collect into v_l_rec;
-- in v_l_rec is list collection of products.
close v_cursor;
end;
未测试代码。
这取决于调用函数的语言或工具。如果从SQL*Plus调用,只需
print <variable_name>
其中是返回函数结果的PL/SQL绑定变量的名称。
如果你是在PL/SQL中,你需要写一个循环来获取…当%未找到时退出…等。有上百万的例子,包括PL/SQL用户指南。
如果你是在Java中,你可以将游标对象强制转换为ResultSet,并在循环中获取其结果。
其他语言也有其他方法,但它们大多都需要遍历游标并获取结果。
所以…这取决于来电者的语言。