Create Table testIndex
(
id
int identity(
1,
1) primary key,
nm
varchar(
100) unique not null,
sex
varchar(
10)
)
create UNIQUE index UQ__testIndex__0DAF0CB0
on testindex(nm)
insert into testindex
select 'aaabbb','m' union all
select 'bbb','w' union all
select 'ccc','w' union all
select 'ddd','m'
準備一組漢字記錄
insert into testindex
select '麥蒂未傷愈中途退出訓練複出時間再度成疑','北京'
go
--建立全文檢索目錄
sp_fulltext_catalog 'abc','create'
go
--建立全文索引(‘表名‘,’建立/刪除‘,’全文檢索目錄名‘,’約束名‘)
sp_fulltext_table 'testindex','create','abc','UQ__testIndex__0DAF0CB0'
go
--添加列到全文索引(‘表名‘,’列名‘,’添加/刪除‘)
sp_fulltext_column 'testindex','nm','add'
go
--建立全文索引
--activate,是啟用表的全文檢索索引能力,也就是在全文檢索目錄中註冊該表
execute sp_fulltext_table 'testindex','activate'
go
--填充全文索引目錄
execute sp_fulltext_catalog 'abc','start_full'
go
--檢查全文檢索目錄填充情況
While fulltextcatalogproperty('abc','populateStatus')<>
0
begin
--如果全文檢索目錄正處於填充狀態,則等待30秒後再檢測一次
waitfor delay '0:0:30'
end
--全文檢索目錄填充完成後,即可使用全文檢索目錄檢索
SELECT * FROM testindex WHERE CONTAINS(nm, '麥蒂')
/*
id nm sex
----------- --------------------------------------------- ------------------------------------------------ ----------
5 麥蒂未傷愈中途退出訓練複出時間再度成疑 北京
(所影響的行數為 1 行)
*/
再次插入新的資料,
insert into testindex
select '麥蒂未傷愈中途退出訓練複出時間再度成疑12121','北京'
go
SELECT * FROM testindex WHERE CONTAINS(nm, '麥蒂')
-----這個時候進行select實際上得不到理想的結果,還是老資料,沒有增加的這一條
/*
id nm sex
----------- --------------------------------------------- ------------------------------------------------ ----------
5 麥蒂未傷愈中途退出訓練複出時間再度成疑 北京
(所影響的行數為 1 行)
*/
go
--填充全文索引目錄
execute sp_fulltext_catalog 'abc','start_full'
go
--檢查全文檢索目錄填充情況
While fulltextcatalogproperty('abc','populateStatus')<>
0
begin
--如果全文檢索目錄正處於填充狀態,則等待30秒後再檢測一次
waitfor delay '0:0:30'
end
--重新填充後就會有想要的結果 了
SELECT * FROM testindex WHERE CONTAINS(nm, '麥蒂')
go
/*
id nm sex
----------- ---------------------------------------------------------------------------------------------------- ----------
6 麥蒂未傷愈中途退出訓練複出時間再度成疑12121 北京
5 麥蒂未傷愈中途退出訓練複出時間再度成疑 北京
(所影響的行數為 2 行)
*/
--清理現場
sp_fulltext_table 'testindex','drop'
go
sp_fulltext_catalog 'abc','drop'
go
drop table testIndex