全文搜索 sql 带参数的服务器
full text search sql server with parameter
我在使用带参数
的 sql 服务器全文时遇到问题
Alter Procedure[dbo].[SelectFullName]
@fullname nvarchar(45)
As
Select*from [dbo][NamePersonTB]
Where CONTAINS (fullname,'"*@fullname*"')
我想对全名使用 SAME LIKE
您通过将 @Fullname 括在单引号中将其用作文字字符串。如果要使用@Fullname 的实际值作为搜索条件,则需要将变量直接传递到 CONTAINS 函数中
另请注意,如果您想利用全文索引,则不能以完全相同的方式执行 %SearchTerm%
。您可以搜索具有匹配前缀但不匹配 suffix/middle 的单词。有关 CONTAINS 通配符 (*) 用法的详细信息,请参阅 MS doc
中的 部分
下面我创建了两种可以设置全文搜索的方法,一种是简单版本,一种是更高级的版本。不确定您的业务需求,但更高级的充分利用全文索引并具有非常简洁的“智能”排名选项
Table 设置
CREATE TABLE NamePersonTB (ID INT IDENTITY(1,1) CONSTRAINT PK_NamePersonTB Primary Key,FullName NVARCHAR(100))
INSERT INTO NamePersonTB
VALUES ('John Smith')
,('Jane Smith')
,('Bill Gates')
,('Satya Nadella')
CREATE FULLTEXT CATALOG ct_test AS DEFAULT;
CREATE FULLTEXT INDEX ON NamePersonTB(FullName) KEY INDEX PK_NamePersonTB;
全文搜索脚本
DECLARE @FullName NVARCHAR(45);
/*Sample searches*/
SET @FullName = 'John Smith' /*Notice John Smith appears first in ranked search*/
--SET @FullName = 'Smith'
--SET @FullName = 'Sm'
--SET @FullName = 'Bill'
DECLARE @SimpleContainsSearchCriteria NVARCHAR(1000)
,@RankedContainsSearchCriteria NVARCHAR(1000)
/*
Below will
1. Parses the words into rows
2. Adds wildcard to end(cannot add wildcard to prefix according to MS doc on CONTAINS)
3. Combines all words back into single row with separator to create CONTAINS search criteria
*/
SELECT @SimpleContainsSearchCriteria = STRING_AGG(CONCAT('"',A.[Value],'*"'),' AND ')
FROM STRING_SPLIT(REPLACE(@Fullname,'"',''),' ') AS A /*REPLACE() removes any double quotes as they will break your search*/
/*Same as above, but uses OR to include more results and will utilize [Rank] so better matches appear first*/
SELECT @RankedContainsSearchCriteria = STRING_AGG(CONCAT('"',A.[Value],'*"'),' OR ')
FROM STRING_SPLIT(REPLACE(@Fullname,'"',''),' ') AS A
/*Included so you can see the search critieria. Should remove in final proc*/
SELECT @Fullname AS FullNameInput
,@SimpleContainsSearchCriteria AS SimpleSearchCriteria
,@RankedContainsSearchCriteria AS RankedContainsSearchCriteria
/*Simple AND match*/
SELECT *
FROM NamePersonTB AS A
WHERE CONTAINS(FullName,@SimpleContainsSearchCriteria)
/*CONTAINSTABLE match alternative. Uses OR criteria and then ranks so best matches appear at the top*/
SELECT *
FROM CONTAINSTABLE(NamePersonTB,FullName,@RankedContainsSearchCriteria) AS A
INNER JOIN NamePersonTB AS B
ON A.[Key] = B.ID
ORDER BY A.[Rank] DESC
示例搜索条件
FullNameInput
SimpleSearchCriteria
RankedContainsSearchCriteria
John Smith
"John*" AND "Smith*"
"John*" OR "Smith*"
简单搜索的输出
ID
FullName
1
John Smith
排名搜索的输出
KEY
RANK
ID
FullName
1
48
1
John Smith
2
32
2
Jane Smith
我在使用带参数
的 sql 服务器全文时遇到问题Alter Procedure[dbo].[SelectFullName]
@fullname nvarchar(45)
As
Select*from [dbo][NamePersonTB]
Where CONTAINS (fullname,'"*@fullname*"')
我想对全名使用 SAME LIKE
您通过将 @Fullname 括在单引号中将其用作文字字符串。如果要使用@Fullname 的实际值作为搜索条件,则需要将变量直接传递到 CONTAINS 函数中
另请注意,如果您想利用全文索引,则不能以完全相同的方式执行 %SearchTerm%
。您可以搜索具有匹配前缀但不匹配 suffix/middle 的单词。有关 CONTAINS 通配符 (*) 用法的详细信息,请参阅 MS doc
部分下面我创建了两种可以设置全文搜索的方法,一种是简单版本,一种是更高级的版本。不确定您的业务需求,但更高级的充分利用全文索引并具有非常简洁的“智能”排名选项
Table 设置
CREATE TABLE NamePersonTB (ID INT IDENTITY(1,1) CONSTRAINT PK_NamePersonTB Primary Key,FullName NVARCHAR(100)) INSERT INTO NamePersonTB VALUES ('John Smith') ,('Jane Smith') ,('Bill Gates') ,('Satya Nadella') CREATE FULLTEXT CATALOG ct_test AS DEFAULT; CREATE FULLTEXT INDEX ON NamePersonTB(FullName) KEY INDEX PK_NamePersonTB;
全文搜索脚本
DECLARE @FullName NVARCHAR(45); /*Sample searches*/ SET @FullName = 'John Smith' /*Notice John Smith appears first in ranked search*/ --SET @FullName = 'Smith' --SET @FullName = 'Sm' --SET @FullName = 'Bill' DECLARE @SimpleContainsSearchCriteria NVARCHAR(1000) ,@RankedContainsSearchCriteria NVARCHAR(1000) /* Below will 1. Parses the words into rows 2. Adds wildcard to end(cannot add wildcard to prefix according to MS doc on CONTAINS) 3. Combines all words back into single row with separator to create CONTAINS search criteria */ SELECT @SimpleContainsSearchCriteria = STRING_AGG(CONCAT('"',A.[Value],'*"'),' AND ') FROM STRING_SPLIT(REPLACE(@Fullname,'"',''),' ') AS A /*REPLACE() removes any double quotes as they will break your search*/ /*Same as above, but uses OR to include more results and will utilize [Rank] so better matches appear first*/ SELECT @RankedContainsSearchCriteria = STRING_AGG(CONCAT('"',A.[Value],'*"'),' OR ') FROM STRING_SPLIT(REPLACE(@Fullname,'"',''),' ') AS A /*Included so you can see the search critieria. Should remove in final proc*/ SELECT @Fullname AS FullNameInput ,@SimpleContainsSearchCriteria AS SimpleSearchCriteria ,@RankedContainsSearchCriteria AS RankedContainsSearchCriteria /*Simple AND match*/ SELECT * FROM NamePersonTB AS A WHERE CONTAINS(FullName,@SimpleContainsSearchCriteria) /*CONTAINSTABLE match alternative. Uses OR criteria and then ranks so best matches appear at the top*/ SELECT * FROM CONTAINSTABLE(NamePersonTB,FullName,@RankedContainsSearchCriteria) AS A INNER JOIN NamePersonTB AS B ON A.[Key] = B.ID ORDER BY A.[Rank] DESC
示例搜索条件
FullNameInput SimpleSearchCriteria RankedContainsSearchCriteria John Smith "John*" AND "Smith*" "John*" OR "Smith*" 简单搜索的输出
ID FullName 1 John Smith 排名搜索的输出
KEY RANK ID FullName 1 48 1 John Smith 2 32 2 Jane Smith