有高手在SQL SERVER数据库编出了代码,oracle怎么用?
--创建函数
create   function   [dbo].[f_splitstr](@SourceSql   Nvarchar(MAX),@StrSeprate   Nvarchar(100))   
  returns   @temp   table(F1  Nvarchar(100))   
  as     
  begin   
  declare   @ch   as  Nvarchar(100)   
  set   @SourceSql=@SourceSql+@StrSeprate     
  while(@SourceSql<>'')   
                  begin   
                  set   @ch=left(@SourceSql,charindex(@StrSeprate,@SourceSql,1)-1)   
  insert   @temp   values(@ch)   
  set   @SourceSql=stuff(@SourceSql,1,charindex(@StrSeprate,@SourceSql,1),'')   
                  end   
  return   
  end  
GO--执行查询
SELECT * FROM test AS t CROSS APPLY dbo.f_splitstr(replace(t.book_name , ',' , ';') , ';') AS fs WHERE FS.F1 <> ''/*执行结果
 id          book_name            F1
----------- -------------------- ----------------------------------------------------------------------------------------------------
1           雷雨,安徒生童话,家           雷雨
1           雷雨,安徒生童话,家           安徒生童话
1           雷雨,安徒生童话,家           家
2           家                    家
3           春;                   春
4           家;春;秋;               家
4           家;春;秋;               春
4           家;春;秋;               秋(8 行受影响)
 */

解决方案 »

  1.   

    with t as
     (select 1 id, 'leiyu,tonghua,jia' book_nm
        from dual
      union all
      select 2 id, 'jia' book_nm
        from dual
      union all
      select 3 id, 'chun;' book_nm
        from dual
      union all
      select 4 id, 'jia;chun;qiu' book_nm
        from dual)
    select *
      from (select id, REGEXP_SUBSTR(book_nm, '[^,]+', 1, LEVEL) STR
              from (select id, replace(book_nm, ';', ',') book_nm from t) t1
            connect by level <= REGEXP_COUNT(book_nm, ',') + 1
                   and id = prior id
                   and prior dbms_random.value is not null) t2
     where t2.str is not null;
      

  2.   


    oracle10 不支持正则REGEXP_COUNT,改了下
    select  t2.str,count(*) as pcount
      from (select id, REGEXP_SUBSTR(book_nm, '[^,]+', 1, LEVEL) STR
              from (select id, replace(book_nm, ';', ',') book_nm from t) t1
            connect by level <= length(book_nm)-length(replace(book_nm, ',') )+ 1
                   and id = prior id
                   and prior dbms_random.value is not null) t2
     where t2.str is not null group by  t2.str  order by  pcount;
    运行很快,1万多条不到1秒。
    谢谢!