日期:2014-05-16  浏览次数:21031 次

ORACLE 调用拆分function的问题 求帮忙
方法如下:
CREATE OR REPLACE TYPE ty_str_split IS TABLE OF VARCHAR2 (4000);
CREATE OR REPLACE FUNCTION fn_split (p_str IN VARCHAR2, p_delimiter IN VARCHAR2)
  RETURN ty_str_split
IS
  j INT := 0;
  i INT := 1;
  len INT := 0;
  len1 INT := 0;
  str VARCHAR2 (4000);
  str_split ty_str_split := ty_str_split ();
BEGIN
  len := LENGTH (p_str);
  len1 := LENGTH (p_delimiter);
  WHILE j < len
  LOOP
  j := INSTR (p_str, p_delimiter, i);
  IF j = 0
  THEN
  j := len;
  str := SUBSTR (p_str, i);
  str_split.EXTEND;
  str_split (str_split.COUNT) := str;
  IF i >= len
  THEN
  EXIT;
  END IF;
  ELSE
  str := SUBSTR (p_str, i, j - i);
  i := j + len1;
  str_split.EXTEND;
  str_split (str_split.COUNT) := str;
  END IF;
  END LOOP;
  RETURN str_split;
END fn_split;


上面是一个拆分函数,我现在要调用来拆分一个表里面的一个字段 我是这样调用的

with emailaddress as(
SELECT variable_value from ledup_variables where variable_type='Email')
select fn_split(variable_value,',') AS ty_str_split from emailaddress


目的是拆分ledup_variables 中的variable_value 字段

但这样执行结果是返回一个数据集 ,点开数据集才是拆分后的数据列表,我怎么才能直接执行出正确的数据?请教

------解决方案--------------------
SQL code
--如果是单行
SQL> WITH emailaddress AS
  2   (SELECT 'abc@sina.com,bbb@yahoo.com,ccc@21cn.com' variable_value FROM dual)
  3  SELECT *
  4    FROM TABLE (SELECT fn_split(variable_value, ',') AS ty_str_split
  5                  FROM emailaddress);

COLUMN_VALUE
--------------------------------------------
abc@sina.com
bbb@yahoo.com
ccc@21cn.com

SQL>

------解决方案--------------------
SQL code
WITH emailaddress AS
 (SELECT variable_value from ledup_variables where variable_type='Email')
SELECT substr(b.variable_value,
              instr(b.variable_value, ',', 1, column_value) + 1,              
              decode(instr(b.variable_value, ',', 1, column_value + 1),
                     0,
                     length(b.variable_value) + 1,
                     instr(b.variable_value, ',', 1, column_value + 1)) -
              instr(b.variable_value, ',', 1, column_value) - 1) s
  FROM (select ','||variable_value variable_value from emailaddress) b,
       TABLE(CAST(MULTISET
                  (SELECT rownum
                     FROM DUAL
                   CONNECT BY rownum <=
                              length(variable_value) -
                              length(REPLACE(variable_value, ','))) AS
                  ty_str_split));