Oracle中函数/过程返回结果集的几种方式:
发布时间:2020-12-12 15:17:39 所属栏目:百科 来源:网络整理
导读:转自:http://blog.csdn.net/feiliu010/article/details/1538822 Oracle中函数/过程返回结果集的几种方式: 以函数return为例,存储过程只需改为out参数即可,在oracle 10g测试通过. (1) 返回游标: return的类型为:SYS_REFCURSOR 之后在IS里面定义变量:curr SYS
转自:http://blog.csdn.net/feiliu010/article/details/1538822 Oracle中函数/过程返回结果集的几种方式: 以函数return为例,存储过程只需改为out参数即可,在oracle 10g测试通过.(1) 返回游标: return的类型为:SYS_REFCURSOR 之后在IS里面定义变量:curr SYS_REFCURSOR; 最后在函数体中写: open cur for select ......; return cur; 例: CREATE OR REPLACE FUNCTION A_Test( orType varchar2 ) RETURN SYS_REFCURSOR is type_curSYS_REFCURSOR; BEGIN OPEN type_cur FOR select col1,col2,col3 from testTable; type_cur; END ; (2)返回table类型的结果集: 首先定义一个行类型: TYPE"SPLIT_ARR" AS OBJECT(nowStr ( 18 )) 其次以此行类型定义一个表类型: TYPE"SPLIT_TAB" TABLE of split_arr; 定义函数(此函数完成字符串拆分功能): GetSubStr( str in , -- 待分割的字符串 splitchar 分割标志 ) return split_tab IS restStr 2000 ) default GetSubStr. ; 剩余的字符串 thisStr ); 取得的当前字符串 indexStr int 临时存放分隔符在字符串中的位置 vsplit_tab: = split_tab(); 返回结果 begin dbms_output.put_line(restStr); while length(restStr) != 0 LOOP << top >> indexStr: instr(restStr,splitchar); 从子串中取分隔符的第一个位置 if indexStr and then 在剩余的串中找不到分隔符 v.extend; v(v. count ): split_arr(Reststr); v; end ; ; 1 -第一个字符便为分隔符,此时去掉分隔符 restStr: substr(restStr, 2 ); goto top or restStr null ; v.extend; thisStr: - 取得当前的字符串 restStr: + -取剩余的字符串 v(v. split_arr(thisStr); LOOP; 在PL/SQL developer中可以直接调用 cursor strcur nowStr Table (GetSubStr( ' 111,222,333, ' )); (3)以管道形式输出: create typerow_type as object(a 10 ),v )); 定义行对象 typetable_type table row_type; 定义表对象 replace function test_fun( a ) table_typepipelined vrow_type; 定义v为行对象类型 for thisrow ( a,b mytable where col1 a col2 b)loop v: row_type(thisrow.a,thisrow.b); pipe row(v); loop; * (test_fun( 123 456 )); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |