Can I use a variable when using ISABOUT?

大城市里の小女人 提交于 2019-12-10 20:49:56

问题


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:


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.



来源:https://stackoverflow.com/questions/12877852/can-i-use-a-variable-when-using-isabout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!