首页 > 技术文章 > sqlserver 实现数据库全文检索

chenjt 2013-08-06 10:46 原文

--在执行该脚本程序之前启动sql server的全文搜索服务,即microsoft search服务 
use huarui_db --打开数据库 
go 
--检查huarui_db是否支持全文索引,如果不支持全文索引,则使用sp_fulltext_datebase打开该功能 
if (select databaseproperty ('huarui_db','IsFulltextEnables'))=0 
execute sp_fulltext_database 'enable' 
--建立全文目录FullText_huarui_db 
execute sp_fulltext_catalog 'FullText_huarui_db','create' 
--为Products表建立全文索引数据元 
execute sp_fulltext_table 'Products','create','FullText_huarui_db','PK_Products' 


--设置全文索引列名 
execute sp_fulltext_column 'Products','Product_name','add' 
execute sp_fulltext_column 'Products','Product_type','add' 
execute sp_fulltext_column 'Products','Category_name','add' 
execute sp_fulltext_column 'Products','Memo1','add' 
--建立全文索引 
--execute sp_fulltext_table 'FullText_huarui_db','activate' 
--填充全文索引目录 
execute sp_fulltext_catalog 'FullText_huarui_db','start_full' 


GO 
--检查全文目录填充情况 
WHILE FulltextCatalogProperty('FullText_huarui_db','PopulateStatus')<>0 
BEGIN 
--如果全文目录正处于填充状态,则等待30秒后再检测一次 
	WAITFOR DELAY '0:0:30'
END 
--全文目录填充完成后,使用全文目录检索 
--查询title列或notes列中包含有database或computer字符串的图书名称 
SELECT * 
FROM Products
where contains(Product_name,'%1806%') 
or contains(Product_type,'%1806%') 

  

推荐阅读