库表结构如下:create table demotab(oid number, namelist varchar2(5000));
里面数据格式如下:
oid|namelist
1|a,b,c
2|d,e,f
...许多行============================
希望写一个函数,能返回以下两列oid, name
1, a
1, b
1, c
2, d
2, e
2, f
...许多行
===========================
不知道是否可行,网上有一个split函数(http://www.cnblogs.com/linbaoji/archive/2009/09/17/1568252.html),只能处理固定的字符串,而且只能返回一列
无法两列多行

解决方案 »

  1.   

    你可以先把 namelist 列拆分成很多列,然后再列转行,这个有很多例子可以搜索借鉴的
      

  2.   

    方法有很多,给你写一种通常的写法吧!SQL> with tmp as
      2  (
      3    select '1|a,b,c' str from dual union all
      4    select '2|d,e,f' str from dual
      5  )
      6  select distinct id, regexp_substr(name,'[^,]+',1,level) name
      7   from (select substr(str,1,instr(str,'|')-1) id,
      8                substr(str,instr(str,'|')+1) name
      9          from tmp
     10        )
     11  connect by level<=length(name)-length(replace(name,',',''))+1
     12  order by id, name;
     
    ID             NAME
    -------------- --------------
    1              a
    1              b
    1              c
    2              d
    2              e
    2              f
     
    6 rows selected
      

  3.   

    SQL> with tmp as
      2  (
      3    select '1|a,b,c' str from dual union all
      4    select '2|d,e,f' str from dual
      5  )
      6  select distinct id, regexp_substr(name,'[^,]+',1,level) name
      7   from (select substr(str,1,instr(str,'|')-1) id,
      8                substr(str,instr(str,'|')+1) name
      9          from tmp
     10        )
     11  connect by level<=length(name)-length(replace(name,',',''))+1
     12  order by id, name;
     
    ID             NAME
    -------------- --------------
    1              a
    1              b
    1              c
    2              d
    2              e
    2              f
     
    6 rows selected
      

  4.   

    谢谢  找人帮我写了一个
    --建立对象类型
    CREATE or replace TYPE mytype AS OBJECT (
    field1 NUMBER,
    field2 VARCHAR2 (50)
    );
    --建立复合类型
    CREATE TYPE mytypelist AS TABLE OF mytype;
    --建立函数
    CREATE OR REPLACE FUNCTION pipelineme2(v_cur sys_refcursor)
    RETURN mytypelist PIPELINED
    IS
      

  5.   

        经验之谈:能用SQL语句解决的优先考虑SQL,其次考虑PL/SQL块,再次考虑存储过程或者函数。
         希望对你有用。