Sql Server全文索引实例(二)
------------激活索引
exec sp_fulltext_table 'product','activate'
exec sp_fulltext_table 'product','start_full'
--检查全文目录填充情况
Whilefulltextcatalogproperty('gf_ft','populateStatus') <>0
begin
--如果全文目录正处于填充状态,则等待5秒后再检测一次
waitfor delay '0:0:5'
END www.2cto.com
exec sp_fulltext_catalog 'gf_ft','start_full'
--检查全文目录填充情况
Whilefulltextcatalogproperty('gf_ft','populateStatus') <>0
begin
--如果全文目录正处于填充状态,则等待5秒后再检测一次
waitfor delay '0:0:5'
END
--------------测试------------------
SELECT*FROM product WHERECONTAINS(productname,'美赞臣')
-----------------------卸载------------------
EXEC sp_fulltext_table 'product','deactivate'
exec sp_fulltext_column 'product', 'productname', 'drop'
EXEC sp_fulltext_table 'product', 'drop'
EXEC sp_fulltext_catalog 'gf_ft', 'stop'
EXEC sp_fulltext_catalog 'gf_ft', 'drop'
******************************************************************
基本知识 www.2cto.com
1. SQL Server7 的DeskTop 版中没有全文本检索。
2. 一个表只能有一个全文本检索。
3. 被检索的表必须有单列的唯一索引。
4. 全文本的索引存储在文件系统中,而非
数据库中。
6. 全文本索引包含在全文本目录( Full-Text Catalog )中,每个数据库可以包含一个或多个目录,但一个目录不能属于多个数据库。
7. 全文本检索只能在真正的表上创建,不能是视图,系统表,临时表。
8. 全文本检索会忽略某些噪音字( noise words),比如英文的a,the,and,中文的'和','是'等等。
9. 如果在查询中包含noise words ,就会引发错误,在应用程序中应去除这些noise words。
启动全文本检索服务。
方法A:在企业管理器中打开Support Services 文件夹,在Full-Text Search 的右键菜单中选择Start。
方法B:在SQL Server Service Manager 的Services 下拉列表中选择Microsoft Search,并单击Start/Continue 按钮。
方法C:使用net start mssearch 的命令行方式。
使用全文本检索向导( Full-Text Indexing Wizard )。
step1. 选择被检索的数据库,在Tools 的菜单中,选择Full-text Indexing,进入欢迎( Welcome )的屏幕,单击next。 www.2cto.com
step2. 选择被检索的表,单击next。
step3. 选择唯一索引,单击next。
step4. 选择被索引的列,单击Add,该列显示在右栏中。单击next。
step5. 选择目录(选择已存在的目录,或创建新的目录),单击next。
step6. 选择或创建population schedule(可选项),单击next。
step7. 单击finish。
使用SQL-DMO (以VB 为例)
step1. 在工程的引用中选择Microsoft SQLDMO Object Library。
step2. 创建SQLServer 对象。
Dim objSQL As New SQLDMO.SQLServer
objSQL.Connect "localhost", "sa", ""
step3. 创建新的目录,并加入到被索引的数据库目录中。
Dim objCatalog As New SQLDMO.FullTextCatalog
使pubs 为全文本检索的数据库
objSQL.Databases("pubs").EnableFullTextCatalogs
创建新的目录
objCatalog.Name = "ftcPubsTest"
将新目录加入到目录集合中
objSQL.Databases("pubs").FullTextCatalogs.Add objCatalog
step4. 在表上创建全文本索引。
Dim objTable As New SQLDMO.Table
指定被索引的表
Set objTable = objSQL.Databases("pubs").Tables("authors")
指定目录名和唯一索引名
objTable.FullTextCatalogName = "ftcPubsTest"
objTable.UniqueIndexForFullText = "UPKCL_auidind"
objTable.FullTextIndex = True
指定被索引的列
objTable.Columns("au_lname").FullTextIndex = True
objTable.Columns("au_fname").FullTextIndex = True
www.2cto.com
激活该表上的全文本索引
objTable.FullTextIndexActive = True
step5. 启动全文本目录
objCatalog.Start SQLDMOFullText_Full
使用存储过程
step1. 使pubs 为全文本检索的数据库
USE Pubs
go
sp_fulltext_database 'enable'
step2. 创建新的目录
sp_fulltext_catalog 'ftcPubsTest','create'
step3. 指定被索引的表
sp_fulltext_table 'authors','create','ftcPubsTest','UPKCL_auidind'
step4. 指定被索引的