按姓氏,名字和名字进行全文搜索

时间:2015-01-09 18:02:22

标签: sql-server view full-text-search

您好我使用 SQL Server ,我想在表格中进行全文搜索

CREATE TABLE Students
(
    id int PRIMARY KEY IDENTITY,
    firstName varchar(MAX) NOT NULL CHECK (firstName != ''),
    secondName varchar(MAX) NULL CHECK (secondName != ''),
    lastName varchar(MAX) NOT NULL CHECK (lastName != ''),
    birthDate datetime2 NOT NULL CHECK (birthDate >= '1.01.1990'),
    recordBookNumber int NOT NULL UNIQUE CHECK (recordBookNumber > 0),
    enrollmentDate datetime2 NOT NULL CHECK (enrollmentDate >= '1.01.1990'),
    groupId int NOT NULL FOREIGN KEY REFERENCES [Groups](id) ON DELETE CASCADE
)

所以我创建了视图StudentsSearchView,Clustered Index,Fulltext目录和全文索引

全文索引在这里:

CREATE FULLTEXT INDEX ON
    dbo.StudentsSearchView (
        firstName LANGUAGE 1033,
        secondName LANGUAGE 1033,
        lastName LANGUAGE 1033
    )
KEY INDEX IDX_SEARCH ON ftCatalog
WITH (
    CHANGE_TRACKING = AUTO, 
    STOPLIST = SYSTEM
)

所以我有一个文本框,我输入的字符串就像John Peter Smith'我希望得到所有 匹配,但我无法弄清楚我需要使用哪个查询。我试试这个

SELECT * FROM
    dbo.StudentsSearchView
WHERE CONTAINS (
    *, '"LastName*" & "FirstName*" & "SecondName*"'
)

但是没有发现任何东西,尽管我已经排在了这一行。

P.S。 LastName,FirstName和SecondName将以编程方式插入

1 个答案:

答案 0 :(得分:1)

我解决了。我的错误是View。这是

CREATE VIEW
    dbo.StudentsSearchView WITH SCHEMABINDING
AS
SELECT
    id, firstName, secondName, lastName
FROM
    dbo.Students
GO

但它必须是这样的:

CREATE VIEW
    dbo.StudentsSearchView (id, Fio) WITH SCHEMABINDING
AS
SELECT
    id, firstName + ' ' + ISNULL(secondName, '') + ' ' + lastName
FROM
    dbo.Students
GO

所以,现在这个Select查询工作

SELECT * FROM
    dbo.StudentsSearchView
WHERE CONTAINS (
    *, '"LastName*" & "FirstName*" & "SecondName*"'
)