有高手在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 行受影响)
*/
--创建函数
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 行受影响)
*/
解决方案 »
- oracle关联统计查询
- SQL> alter system dump datafile 5 block 3; 是什么意思啊
- Oracle数据导入SQLSERVER报错
- 9i,系统崩溃, .ora文件由于在C盘丢失,其余文件均在D,求恢复
- 最菜的问题 装了10g并建立了数据库 要想表已经有SQL文件 应该在什么地方执行
- oracle10g如何修改字符集
- Oracle导出多个表时如何判断列名是否存在
- oracle 调优 statspack报告分析
- 如何在oracle8i的dba下导出导入数据表呢?
- sqlplus worksheet 的问题?在线等!
- 如何在oracle中得到一个数字类型的周几
- 存储过程中,传递字符串类型参数,提示java.sql.SQLException: ORA-00904: "采购部": 标识符无效
(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;
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秒。
谢谢!