学习陈亮老师的课程《如何在大量文字中加快搜索关键词的速度?–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')