建全文索引的简单例子

简单的举个例子:


建立全文索引:

write by tootto(csdn) SQL code
   
   
create fulltext index on TestTable ( [ name ] , [ Description ] Language 3076 -- 中文语言索引ID )



自己写一个自定义函数,dbo.GetFullTextSearchText     
参数:varchar(xxxx)
输出:varchar(xxxx)
作用:将输入字符串转换成全文索引查询格式. 这个很好写,就不浪费时间了。
如输入参数是:'你好', 则输出结果为: "你好*" OR "你好" (注意输出结果要完全符合输出结果的格式,要带引号,星号还有 OR,要完全一样)

具体全文索引查询格式参考:http://msdn.microsoft.com/en-us/library/ms187787.aspx


全文索引查询,分页,排序存储过程:

SQL code
   
   
create procedure TestFullTextSearch ( @testText1 nvarchar ( 200 ), @testText2 nvarchar ( 200 ) -- 标准分页参数 @pageNumber int , @rowEachPage int , @minCurrentPageRowId int , @maxCurrentPageRowId int , @sortColumn nvarchar ( 150 ), @sortOrder bit ) as begin -- 定义最终结果表 declare @ResultTable table ( Id int , [ Name ] nvarchar ( 1000 ), [ Description ] nvarchar ( 1000 ) ) insert into @ResultTable select Id, [ Name ] , [ Description ] from dbo.TestTable tt where @testText1 is null or contains (tt.Name, dbo.GetFullTextSearchText( @testText1 )) -- 全文索引查询 and @testText2 is null or contains (tt.Description, dbo.GetFullTextSearchText( @testText2 )) -- 分页 排序 select @minCurrentPageRowId = ( @pageNumber * @rowEachPage ) + 1 , @maxCurrentPageRowId = ( @pageNumber + 1 ) * @rowEachPage if @sortOrder = 0 -- 升序 select * from ( select row_number() over ( order by case @sortColumn when ' id ' then id end when ' name ' then name end ) as RowId, * from @ResultTable ) as t where RowId between @minCurrentPageRowId and @maxCurrentPageRowId else -- 降序 select * from ( select row_number() over ( order by case @sortColumn when ' id ' then id end desc when ' name ' then name end desc ) as RowId, * from @ResultTable ) as t where RowId between @minCurrentPageRowId and @maxCurrentPageRowId end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值