Jika Anda tidak menentang penggunaan tabel temp, dan EXEC (dan saya menyadari itu besar jika), Anda dapat melakukan hal berikut:
DECLARE @KeywordList VARCHAR(MAX), @KeywordQuery VARCHAR(MAX)
SELECT @KeywordList = STUFF ((
SELECT '"' + Keyword + '" OR '
FROM FTS_Keywords
FOR XML PATH('')
), 1, 0, '')
SELECT @KeywordList = SUBSTRING(@KeywordList, 0, LEN(@KeywordList) - 2)
SELECT @KeywordQuery = 'SELECT RecordID, Document FROM FTS_Demo_2 WHERE CONTAINS(Document, ''' + @KeywordList +''')'
--SELECT @KeywordList, @KeywordQuery
CREATE TABLE #Results (RecordID INT, Document NVARCHAR(MAX))
INSERT INTO #Results (RecordID, Document)
EXEC(@KeywordQuery)
SELECT * FROM #Results
DROP TABLE #Results
Ini akan menghasilkan kueri seperti:
SELECT RecordID
,Document
FROM FTS_Demo_2
WHERE CONTAINS(Document, '"red" OR "green" OR "blue"')
Dan hasilnya seperti ini:
RecordID Document
1 one two blue
2 three red five