标签:
Oracle中函数/过程返回结果集的3种方式,现总结如下:
CREATEOR
REPLACEFUNCTION A_Test(
orTypevarchar2
)RETURN SYS_REFCURSOR
is
type_cur SYS_REFCURSOR;
BEGIN
OPEN type_curFOR
select col1,col2,col3from
testTable ;
RETURN type_cur;
END;
CREATEOR
REPLACE TYPE "SPLIT_ARR" AS OBJECT(nowStrvarchar2(18))
CREATE
OR REPLACE TYPE "SPLIT_TAB"AS
TABLEof split_arr;
CREATEOR
REPLACEFUNCTION GetSubStr(
str
invarchar2,--待分割的字符串
splitcharin
varchar2--分割标志
)
return split_tab
IS
restStrvarchar2(2000)default
GetSubStr.str;--剩余的字符串
thisStrvarchar2(18);--取得的当前字符串
indexStrint;--临时存放分隔符在字符串中的位置
v split_tab := split_tab();--返回结果
begin
dbms_output.put_line(restStr);
while length(restStr)!=
0
LOOP
<<top>>
indexStr := instr(restStr,splitchar);--从子串中取分隔符的第一个位置
if indexStr
= 0and length(restStr)!=
0 then--在剩余的串中找不到分隔符
begin
v.extend;
v(v.count) :=
split_arr(Reststr);
return v;
end;
end
if;
if indexStr
= 1then---第一个字符便为分隔符,此时去掉分隔符
begin
restStr := substr(restStr,2);
goto
top;
end;
end
if;
if length(restStr)=
0or restStr
is nullthen
return v;
end
if;
v.extend;
thisStr := substr(restStr,1,indexStr-
1);--取得当前的字符串
restStr := substr(restStr,indexStr+
1);---取剩余的字符串
v(v.count) :=
split_arr(thisStr);
END LOOP;
return v;
end;
cursor strcuris
select nowStrfrom
Table(GetSubStr(‘111,222,333,,,‘,‘,‘));
create type row_typeas
object(a varchar2(10), vvarchar2(10));--定义行对象
create type table_typeas
tableof row_type;
--定义表对象
create
or replacefunction test_fun(
ain
varchar2,bin
varchar2
)
return table_type pipelined
is
v row_type;--定义v为行对象类型
begin
for thisrow
in (select a, bfrom mytable
where col1=aand col2
= b) loop
v := row_type(thisrow.a, thisrow.b);
pipe row (v);
end loop;
return;
end;
select
* fromtable(test_fun(‘123‘,‘456‘));标签:
原文地址:http://blog.csdn.net/yanyu529584640/article/details/51271868