• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

ntext搜索关键字

发布: 2007-7-02 11:08 | 作者: admin | 来源: | 查看: 20次 | 进入软件测试论坛讨论

领测软件测试网
/*--ntext搜索

 按 tb 表中的 keyword 在 ta 中查找 content 列出每个 keyword 在 content 中的具体位置--邹建 2004.07--*/

--测试数据create table ta(id int identity(1,1),content ntext)insert ta select @#我是中国人我是中国人@#union all select @#中国人民爱中国 中国人民爱中国 中国人民爱中国 中国人民爱中国@#

create table tb(keyword nvarchar(100))insert tb select @#中@#union all select @#中国@#go

/*=================处理========================*/if exists (select * from dbo.sysobjects where id = object_id(N@#[序数表]@#) and OBJECTPROPERTY(id, N@#IsUserTable@#) = 1)drop table [序数表]GO

--为了效率,所以要一个辅助表配合select top 4000 id=identity(int,1,1) into 序数表 from syscolumns a,syscolumns balter table 序数表 add constraint pk_id_序数表 primary key(id)go

--创建处理的存储过程create proc p_searchascreate table #t(id int,keyword nvarchar(100),position int)

declare @s Nvarchar(4000),@keyword nvarchar(100)declare @id int,@i int,@ilen int

declare tb cursor local forselect a.id,b.keyword,position=charindex(b.keyword,a.content)-1,ilen=4000-len(b.keyword)from ta a,tb bwhere charindex(b.keyword,a.content)>0

open tbfetch tb into @id,@keyword,@i,@ilenwhile @@fetch_status=0begin select @s=substring(content,@i+1,4000) from ta where id=@id while @s<>@#@# begin  insert #t(id,keyword,position)  select @id,@keyword,id+@i  from 序数表  where charindex(@keyword,@s,id)=id

  select @i=@i+@ilen,@s=substring(content,@i+1,4000)  from ta where id=@id end  fetch tb into @id,@keyword,@i,@ilenendclose tbdeallocate tbselect * from #tgo

--调用示例exec p_searchgo

--删除测试drop table 序数表,ta,tbdrop proc p_search

/*--测试结果

id          keyword   position  ----------- --------- ----------1           中        31           中        81           中国      31           中国      82           中        12           中        62           中        92           中        142           中        172           中        222           中        252           中        302           中国      12           中国      62           中国      92           中国      142           中国      172           中国      222           中国      252           中国      30

(所影响的行数为 20 行)--*/

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网