SQL Server的全文检索

学习陈亮老师的课程《如何在大量文字中加快搜索关键词的速度?–SQL Server的全文检索》 
将SQL 整理了下:

--检查服务器全文检索服务是否安装
if serverproperty('IsFullTextInstalled') =1
   select '已安装' as 全文索引服务
else
   select '未安装' as 全文索引服务

--检查数据库全文检索服务是否启用
if databaseproperty('Northwind','IsFullTextEnabled') =1
   select '已激活' as 全文索引服务
else
   select '未激活' as 全文索引服务

--===============
--返回全文目录函数
--===============
if object_id('fn_getFTCName') <> 0
 drop function dbo.fn_getFTCName
GO
Create Function dbo.fn_getFTCName
(
  @ftc_id int
) returns varchar(128)
as
begin
declare @ftc_name varchar(128)

 if @ftc_id = 0
    select @ftc_name='--'
        else
           select @ftc_name=name from sysfulltextcatalogs where ftcatid=@ftc_id

        return @ftc_name
end
GO

--判断数据表是否启用全文索引
select [name],
     --表是否启用全文索引
     case Objectproperty([id],'tablehasactivefulltextindex')
     when 1 then '已激活'
     when 0 then '未激活' end  As 全文索引,
     --表的全文目录
     dbo.fn_getFTCName(Objectproperty([id],'TableFulltextCatalogid')) as 全文目录,
     --表的当前填充操作
     case objectproperty([id],'tablefulltextpopulatestatus')
     when 0 then '不填充'
     when 1 then '完全填充'
     when 2 then '增量填充' end as 当前填充操作            
from Sysobjects Where xtype='U'

select object_name([id]) as 表名,[name] as 全文索引键
from sysindexes where indexproperty([id],[name],'IsFulltextKey')=1

--激活数据库全文索引
Exec sp_fulltext_database 'enable'  --'disable'

--创建全文目录
Exec sp_fulltext_catalog 'FTC_Northwind','create','c:/FTC'

--指定全文索引表
Exec sp_fulltext_table 'Categories','create','FTC_Northwind','PK_Categories'
Exec sp_fulltext_table 'Employees','create','FTC_Northwind','PK_Employees'

--指定表的全文索引列
Exec sp_fulltext_column 'Employees','Notes','add',0x0409
Exec sp_fulltext_column 'Categories','Description','add',0x0409

--完全填充全文索引
Exec sp_fulltext_catalog 'FTC_Northwind','start_full'

--增量填充全文索引(需要有时间戳列)
Exec sp_fulltext_catalog 'FTC_Northwind','start_incremental'

--重建全文索引(需执行完全填充)
Exec sp_fulltext_catalog 'FTC_Northwind','rebuild'

--======================
--用contains查询
--======================
select * from Employees where contains(*,'Education')

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值