Domanda

Perhaps I'm missing the obvious, but I've spent all day searching and searching with little luck for the answer to this problem. Perhaps I'm using the incorrect search terms? Never-the-less, this is what I'm trying to do.

I have a table with a FULL-TEXT Index set up on it. When I run the following, I am granted the expected results:

SELECT
    A.*,
    KEY_TBL.[Rank]
FROM
[dbo].[TableA] A
INNER JOIN
    CONTAINSTABLE([dbo].[TableA], *, 'Jon OR Smith'
    ,5
    ) AS KEY_TBL
    ON A.[Id] = KEY_TBL.[Key]
ORDER BY
    KEY_TBL.[Rank] DESC

No problem there. What I want to do however, is pass 'Jon OR Smith' (or any other phrase searched by the end-user) into the query as an input parameter of a Stored Procedure. But when I try the following, I get no results:

declare @search_phrase VARCHAR;
set @search_phrase = 'Jon OR Smith'

SELECT
    A.*
    KEY_TBL.[Rank]
FROM
[dbo].[TableA] A
INNER JOIN
    CONTAINSTABLE([dbo].[TableA], *, @search_phrase
    ,5
    ) AS KEY_TBL
    ON A.[Id] = KEY_TBL.[Key]
ORDER BY
    KEY_TBL.[Rank] DESC

I can't help but feel I'm missing the obvious here. I've tried doing SET @search_phrase = N'Jon OR Smith' but that also yielded no results. In SSMS I get a message stating "Informational: The full-text search condition contained noise word(s)". Whilst I expect this due to OR being a stop word, I need the query to work (without affecting / turning off stop words). I can't help but feel that this should be something lots of people want to do / have already done and yet I've found no articles on it.

It should be noted that there may be one or more words within the search phrase, I will do the preprocessing before sending the completed search phrase to the Stored Procedure to add the necessary "OR"'s.

Can anyone point out where I'm going wrong please?

È stato utile?

Soluzione

It looks like it was me, similar suggestions on posting this question led me to the answer. For anyone else that comes across this "odd" behaviour, you need to ensure that each word is separately double-quoted.

declare @search_phrase VARCHAR(50);
set @search_phrase = N'"Jon" OR "Smith"'

SELECT
    A.*
    KEY_TBL.[Rank]
FROM
[dbo].[TableA] A
INNER JOIN
    CONTAINSTABLE([dbo].[TableA], *, @search_phrase
    ,5
    ) AS KEY_TBL
    ON A.[Id] = KEY_TBL.[Key]
ORDER BY
    KEY_TBL.[Rank] DESC
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top