您可以使用强类型游标及其行类型:
-- example data
create table t1(pk number not null primary key, val varchar2(30));
create table t2(
pk number not null primary key,
t1_fk references t1(pk),
val varchar2(30));
insert into t1(pk, val) values(1, 'value1');
insert into t2(pk, t1_fk, val) values(1, 1, 'value2a');
insert into t2(pk, t1_fk, val) values(2, 1, 'value2b');
declare
cursor cur is
select t1.*, t2.val as t2_val
from t1
join t2 on t1.pk = t2.t1_fk;
function get_data(arg in pls_integer) return cur%rowtype is
l_result cur%rowtype;
begin
select t1.*, t2.val as t2_val
into l_result
from t1
join t2 on t1.pk = t2.t1_fk
where t2.pk = arg;
return l_result;
end;
begin
dbms_output.put_line(get_data(2).t2_val);
end;
更新:您可以轻松地将游标和函数包装在 PL/SQL 包中:
create or replace package pkg_get_data as
cursor cur is
select t1.*, t2.val as t2_val
from t1
join t2 on t1.pk = t2.t1_fk;
function get_data(arg in pls_integer) return cur%rowtype;
end;
(包体省略)