create table params as
select 'key'||level key, 'value'||level value , 'memo about this param' memo
from dual connect by level<=3;
/
create or replace package democursor as
type paramtype is record (key varchar2 (8), value varchar2 (16));
type weakcurtype is ref cursor;
type strongcurtype is ref cursor return paramtype;
type paramstype is table of paramtype;
function getParam (key varchar2) return paramtype;
procedure openParams (cur out strongcurtype);
function getParams (likevalue varchar2) return paramstype;
end;
/
create or replace package body democursor as
function getParam (key varchar2) return paramtype is
param paramtype;
begin
select key, value into param
from params
where key = getParam.key;
return param;
exception when no_data_found then null;
end getParam;
procedure openParams (cur out strongcurtype) is
begin
open cur for
select key, value
from params order by key;
end openParams;
function getParams (likevalue varchar2) return paramstype is
cur weakcurtype;
params paramstype;
begin
open cur for select key, value from params
where value like likevalue||'%' order by key;
fetch cur bulk collect into params;
close cur;
return params;
end;
end;
/
使用包中的游标和函数的各种情况的示例:
var rc refcursor;
declare
param1 democursor.paramtype;
param2 democursor.paramtype;
params democursor.paramstype;
function getParam (nth int) return democursor.paramtype is
cur democursor.strongcurtype;
ret democursor.paramtype;
begin
democursor.openParams (cur);
<<fechnth>> loop
fetch cur into ret;
exit fechnth when cur%rowcount = nth or cur%notfound;
end loop;
return ret;
end;
begin
param1 := democursor.getParam (key=>'key1');
param2 := getParam (nth=>2);
params := democursor.getParams (likevalue=>'val');
open :rc for
select 'got first param' what, param1.key||'='||param1.value param from dual union all
select 'got nth=2 param' what, param2.key||'='||param2.value param from dual union all
select 'got val% params' what, params.key||'='||params.value from table (params) params;
end;
/
TS 在问题和评论中提到的完整引用:
我敢简单解释一下:应该少注意显式和隐式游标之间的选择,最好注意在批处理函数中包含重复请求,从而对用户隐藏它们。
巩固书中介绍的材料的实际示例:
这些示例将帮助您了解如何使用两种类型的游标:显式游标和隐式游标,在显式游标具有结果数据集的强类型和弱类型的情况下。
测试数据和包装规格:
使用包中的游标和函数的各种情况的示例:
工作成果:
上面的可重现示例已放在db<>fiddle上进行修改,以便尝试更好地理解使用游标的某些方面。