TSQL - connection using full-text containers

I currently have the following select statement, but I want to go to full-text search in the Keywords column. How do I rewrite this to use CONTAINS?

SELECT MediaID, 50 AS Weighting
FROM Media m JOIN @words w ON m.Keywords LIKE '%' + w.Word + '%'

@words is a table variable filled with words I want to find:

DECLARE @words TABLE(Word NVARCHAR(512) NOT NULL);
+3
source share
2 answers

If CONTAINS allows a variable or column, you could use something like this.

SELECT MediaID, 50 AS Weighting
FROM Media m
JOIN @words w ON CONTAINS(m.Keywords, w.word)

However, according to the Online Online book for SQL Server CONTAINS , it is not supported. Therefore, there is no way to do this.

Ref: (column_name appears only in the first CONTAINS parameter)

CONTAINS
( { column_name | ( column_list ) | * } 
  ,'<contains_search_condition>'     
[ , LANGUAGE language_term ]
) 
+2

temp EXEC ( , ), :

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

:

SELECT   RecordID
        ,Document 
FROM    FTS_Demo_2 
WHERE CONTAINS(Document, '"red" OR "green" OR "blue"')

:

RecordID    Document
1   one two blue
2   three red five
+4

Source: https://habr.com/ru/post/1795446/


All Articles