简单的举个例子:
建立全文索引:
-
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