此答案已重建,以解決您的問題,使得多個字符串必須跨字段存在。請注意,在CompanyContactLink鏈接表中的單個鍵:
CREATE FULLTEXT CATALOG CompanyContact WITH ACCENT_SENSITIVITY = OFF
GO
CREATE TABLE Contact (ContactKey INT IDENTITY, FirstName VARCHAR(20) NOT NULL, LastName VARCHAR(20) NOT NULL)
ALTER TABLE Contact ADD CONSTRAINT PK_Contact PRIMARY KEY NONCLUSTERED (ContactKey)
CREATE TABLE Company (CompanyKey INT IDENTITY, CompanyName VARCHAR(50) NOT NULL)
ALTER TABLE Company ADD CONSTRAINT PK_Company PRIMARY KEY NONCLUSTERED (CompanyKey)
GO
CREATE TABLE CompanyContactLink (CompanyContactKey INT IDENTITY NOT NULL, CompanyKey INT NOT NULL, ContactKey INT NOT NULL)
GO
INSERT INTO Contact (FirstName, LastName) VALUES ('Dipper', 'Pines')
INSERT INTO Contact (FirstName, LastName) VALUES ('Mabel', 'Pines')
INSERT INTO Contact (FirstName, LastName) VALUES ('Stanley', 'Pines')
INSERT INTO Contact (FirstName, LastName) VALUES ('Soos', 'Ramirez')
INSERT INTO Contact (FirstName, LastName) VALUES ('Wendy', 'Corduroy')
INSERT INTO Contact (FirstName, LastName) VALUES ('Sheriff', 'Blubs')
INSERT INTO Contact (FirstName, LastName) VALUES ('Bill', 'Cipher')
INSERT INTO Contact (FirstName, LastName) VALUES ('Pine Dip', 'Nobody')
INSERT INTO Contact (FirstNAme, LastName) VALUES ('Nobody', 'Pine Dip')
INSERT INTO Company (CompanyName) VALUES ('Mystery Shack')
INSERT INTO Company (CompanyName) VALUES ('Greesy Diner')
INSERT INTO Company (CompanyName) VALUES ('Watertower')
INSERT INTO Company (CompanyName) VALUES ('Manotaur Cave')
INSERT INTO Company (CompanyName) VALUES ('Big Dipper Watering Hole')
INSERT INTO Company (CompanyName) VALUES ('Lost Pines Dipping Pool')
GO
INSERT INTO CompanyContactLink Values (3, 5), (1, 1), (1, 2), (1, 3), (1, 4), (1,5), (5,1), (3,1), (4,1)
GO
CREATE FULLTEXT INDEX ON Contact (LastName, FirstName)
KEY INDEX PK_Contact
ON CompanyContact
WITH STOPLIST = SYSTEM
CREATE FULLTEXT INDEX ON Company (CompanyName)
KEY INDEX PK_Company
ON CompanyContact
WITH STOPLIST = SYSTEM
GO
CREATE VIEW CompanyContactView
WITH SCHEMABINDING
AS
SELECT
CompanyContactKey,
CompanyName,
FirstName,
LastName
FROM
dbo.CompanyContactLink
INNER JOIN dbo.Company ON Company.CompanyKey = CompanyContactLink.CompanyKey
INNER JOIN dbo.Contact ON Contact.ContactKey = CompanyContactLink.ContactKey
GO
CREATE UNIQUE CLUSTERED INDEX idx_CompanyContactView ON CompanyContactView (CompanyContactKey);
GO
CREATE FULLTEXT INDEX ON CompanyContactView (CompanyName, LastName, FirstName)
KEY INDEX idx_CompanyContactView
ON CompanyContact
WITH STOPLIST = SYSTEM
GO
-- Wait a few moments for the FULLTEXT INDEXing to take place.
-- Check to see how the index is doing ... repeat the following line until you get a zero back.
DECLARE @ReadyStatus INT
SET @ReadyStatus = 1
WHILE (@ReadyStatus != 0)
BEGIN
SELECT @ReadyStatus = FULLTEXTCATALOGPROPERTY('CompanyContact', 'PopulateStatus')
END
SELECT
CompanyContactView.*
FROM
CompanyContactView
WHERE
FREETEXT((FirstName,LastName,CompanyName), 'Dipper') AND
FREETEXT((FirstName,LastName,CompanyName), 'Shack')
GO
併爲你的榜樣,爲了與溫迪在沃特陶:
SELECT
CompanyContactView.*
FROM
CompanyContactView
WHERE
FREETEXT((FirstName,LastName,CompanyName), 'Wendy') AND
FREETEXT((FirstName,LastName,CompanyName), 'Watertower')
GO
你能分享有關模型的一些信息?到目前爲止,我的理解是,您有兩個表格:聯繫人和公司,並且每個表都包含ContactKey。我猜測,ContactKey不是您正在搜索的值。 – Paurian
@Paurian,請參閱我的更新,並告訴我您是否需要更多信息。謝謝。 – adam0101
@BogdanSahlean引發錯誤,指出CompanyContact不能成爲CONTAINSTABLE查詢的一部分,因爲它不是全文索引的,而且我也無法向它添加全文索引,因爲該表中沒有文本。 – adam0101