Can I use a variable when using ISABOUT?

603 views Asked by At

I'm trying to use a stored procedure to create a table that ranks posts by taking a topic name and using keywords and weights associated with that topic name to determine how they should be ranked. I've been trying to use CONTAINSTABLE and ISABOUT, but I'm having trouble with putting the keywords and weights into the ISABOUT statement. I've tried converting the keywords and weights from the table they're in into a varchar variable, and putting that variable into the ISABOUT statement, but when I run the SP, the resulting table is empty, so I'm assuming the variable isn't working and I'm not sure where to go from here.

Here's what I have so far:

CREATE PROCEDURE rankingSP (@Topic varchar(30))
AS
BEGIN
    --creates table to display when sp is executed  
    CREATE TABLE #rankingTable(
    Post_ID     int,
    Post_cont   varchar(max),
    [Rank]      decimal(18,2))

    --creates string with keywords and weights
    DECLARE @keywordString varchar(max)
    SELECT @keywordString = COALESCE(@keywordString + ',','') 
    + Keyword + ' ' + 'WEIGHT' + '(' + CONVERT(varchar,K_weight) + ')'
    FROM Keyword
    PRINT @keywordString

    --inserts rankings into rankingTable
    INSERT INTO #rankingTable
    SELECT
    p.[Post_ID],
    p.[Post_cont],
    ct.[RANK]
    FROM CONTAINSTABLE
    (
    Post,
    Post_cont,
    N'ISABOUT (@keywordString)'
    ) ct
    INNER JOIN Post p
    ON ct.[KEY] = p.Post_ID
    ORDER BY ct.[RANK] DESC;

    --displays the ranking table
    SELECT * FROM #rankingTable
    ORDER BY [Rank]DESC
END
1

There are 1 answers

3
RThomas On BEST ANSWER

It seems to me that because of the way your passing the search condition the sql engine doesn't recognize it as variable but simply a string. It's been awhile since I did anything with CONTAINSTABLE but I think it should work if you try it like this.

--- snippet
FROM CONTAINSTABLE
(
 Post,
 Post_cont,
 N'ISABOUT (' + @keywordString + ')'
)
ct
INNER JOIN Post p
  ON ct.[KEY] = p.Post_ID
  ORDER BY ct.[RANK] DESC;

Further, you may need to pass the "" quotes. Here is a similar question that demonstrates the same concept.