首先,你需要啓用全文搜索索引在生產服務器上,所以如果這不在範圍內,你不會想要這樣做。
但是,如果已經準備好了,全文搜索就相對簡單了。
T-SQL具有用於全文搜索4個謂詞:
- FREETEXT
- FREETEXTTABLE
- CONTAINS
- CONTAINSTABLE
FREETEXT是最簡單的,並且可以做到像這樣:
SELECT UserName
FROM Tbl_Users
WHERE FREETEXT (UserName, 'bob')
Results:
JimBob
Little Bobby Tables
FREETEXTTABLE與FreeTEXT的工作方式相同,只是它將結果作爲表格返回。
T-SQL的全文搜索的真正動力來自於CONTAINS(和CONTAINSTABLE)斷言...這一個是巨大的,所以我就貼在它的用法:
CONTAINS
({ column | * } , '<contains_search_condition>'
)
<contains_search_condition> ::=
{ <simple_term>
| <prefix_term>
| <generation_term>
| <proximity_term>
| <weighted_term>
}
| { (<contains_search_condition>)
{ AND | AND NOT | OR } <contains_search_condition> [ ...n ]
}
<simple_term> ::=
word | " phrase "
< prefix term > ::=
{ "word * " | "phrase * " }
<generation_term> ::=
FORMSOF (INFLECTIONAL , <simple_term> [ ,...n ])
<proximity_term> ::=
{ <simple_term> | <prefix_term> }
{ { NEAR | ~ } { <simple_term> | <prefix_term> } } [ ...n ]
<weighted_term> ::=
ISABOUT
({ {
<simple_term>
| <prefix_term>
| <generation_term>
| <proximity_term>
}
[ WEIGHT (weight_value) ]
} [ ,...n ]
)
這意味着你可以寫的查詢,如:
SELECT UserName
FROM Tbl_Users
WHERE CONTAINS(UserName, '"little*" NEAR tables')
Results:
Little Bobby Tables
祝你好運:)
我已經投票贊成並將其作爲答案,不僅因爲它是一個很好而詳細的迴應,而且也是針對xkcd參考。贏得。 – 2008-10-14 19:50:39