ntext搜尋關鍵字

2021-06-16 01:00:52 字數 1937 閱讀 1845

/*--

ntext搜尋

按tb表中的keyword在ta中查詢content 列出每個keyword在content中的具體位置--鄒建2004.07--*/

--測試資料createtableta(idintidentity(1,1),content

ntext)inserttaselect'我是中國人我是中國人'unionallselect'中國人民愛中國中國人民愛中國中國人民愛中國中國人民愛中國'

createtabletb(keywordnvarchar(100))inserttbselect'中'unionallselect'中國'go

/****************==處理********************====*/ifexists(select*fromdbo.sysobjectswhereid=object_id(n'[序數表]')andobjectproperty(id,n'isusertable')=1)droptable[序數表]go

--為了效率,所以要乙個輔助表配合selecttop4000id=identity(int,1,1)into序數表fromsyscolumnsa,syscolumnsbaltertable序數表addconstraintpk_id_序數表primarykey(id)go

--建立處理的儲存過程createprocp_searchascreatetable#t(idint,keywordnvarchar(100),positionint)

declare@snvarchar(4000),@keywordnvarchar(100)declare@idint,@iint,@ilenint

declaretbcursorlocalforselecta.id,b.keyword,position=charindex(b.keyword,a.content)-1,ilen=4000-len(b.keyword)fromtaa,tbbwherecharindex(b.keyword,a.content)>0

opentbfetchtbinto@id,@keyword,@i,@ilenwhile@@fetch_status=0begin select@s=substring(content,@i+1,4000) fromtawhereid=@id while@s<>'' begin  insert#t(id,keyword,position)  select@id,@keyword,id+@i  from序數表  wherecharindex(@keyword,@s,id)=id

select@i=@i+@ilen,@s=substring(content,@i+1,4000)  fromtawhereid=@id end  fetchtbinto@id,@keyword,@i,@ilenendclosetbdeallocatetbselect*from#tgo

--呼叫示例execp_searchgo

--刪除測試droptable序數表,ta,tbdropprocp_search

/*--測試結果

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

ntext搜尋關鍵字

選擇自 zjcxc 的 blog ntext搜尋 按 tb 表中的 keyword 在 ta 中查詢 content 列出每個 keyword 在 content 中的具體位置 鄒建 2004.07 測試資料 create table ta id int identity 1,1 content n...

ntext搜尋關鍵字

ntext搜尋 按 tb 表中的 keyword 在 ta 中查詢 content 列出每個 keyword 在 content 中的具體位置 鄒建 2004.07 引用請保留此資訊 測試資料 create table ta id int identity 1,1 content ntext ins...

ntext搜尋關鍵字

ntext搜尋 按 tb 表中的 keyword 在 ta 中查詢 content 列出每個 keyword 在 content 中的具體位置 鄒建 2004.07 引用請保留此資訊 測試資料 create table ta id int identity 1,1 content ntext ins...