Oracle实现类split函数的方

关键字:Oracle实现类split函数的方
项目里需要保存结构数据,批量传到后他进行保存,为了减小数据量,子集拼装的格式,使用存储过程进行保存。保存的过程中需要对数据解析。但是oracle没有Java中split类似的函数。从网上找了一个,也补全了一下。

CREATE OR REPLACE TYPE t_split_100 IS TABLE OF VARCHAR2(100);

create or replace function f_split_100(p_str_all in varchar2,
                                            p_str_gap in varchar2)
  return t_split_100 is
  v_ntb_allstring t_split_100;

  str_unit varchar2(100);
  str_char varchar2(100);

  i_str_length number;
  i_str_index  number;

begin
  v_ntb_allstring := t_split_100();

  i_str_length := length(p_str_all);

  i_str_index := 1;

  while (i_str_index <= i_str_length) loop
    str_char := substr(p_str_all, i_str_index, 1);

    if (str_char = p_str_gap) then

      if (str_unit is not null) then
        v_ntb_allstring.extend(1);
        v_ntb_allstring(v_ntb_allstring.count) := str_unit;
        str_unit := null;
      end if;

    else
      str_unit := str_unit || str_char;

      if (i_str_index = i_str_length) then
        v_ntb_allstring.extend(1);
        v_ntb_allstring(v_ntb_allstring.count) := str_unit;
        str_unit := '';
      end if;

    end if;

    i_str_index := i_str_index + 1;
  end loop;

  return(v_ntb_allstring);
end;
使用方法f_split_100(‘a|b|c|d’, ‘|’),返回的是Collection。
查看结果的方法 select * from table(f_split_100(‘a|b|c|d’, ‘|’));
Collection使用方法如下

EXISTS 该函数返回集合中第一个元素的索引,如果集合为空,返回NULL Collection.EXISTS(index
COUNT Collection.COUNT Collection.COUNT
DELETE 该过程从嵌套表中删除一个或多个或合部元素 Table_name.DELETE 删除所有元素
Table_name.delete(index)删除指定索引的记录
Table_name.delete(start_index,end_index)删除区间内元素
FIRST 返回集合第一个元素索引,如果集合为空,返回NULL Collection.FIRST
LAST 返回集合中最后一个元素索引,如果集合为空,返回NULL Collection. LAST
NEXT 返回集合中最后一个元素索引,如果集合为空,返回NULL Collection. NEXT
PRIOR Collection. PRIOR Collection. PRIOR
LIMIT 返回varray中创建元素的最大个数 Collection. LIMIT
EXTENDS 该过程在集合的末尾添加新的元素 Collection.EXTEND添加一个NULL元素;Collection.extends(n)添加N个NULL元素,Collection.extend(n,index)添加由index指定位置上元素的n个副表
TRIM Collection.TRIM 删除最后一个元素
Collection.TRIM(n)删除最后N个元素 Collection.TRIM 删除最后一个元素
Collection.TRIM(n)删除最后N个元素

猜你喜欢

转载自zhaoshijie.iteye.com/blog/2202918