1

我有一个搜索@searchString 的工作FREETEXTTABLE 查询。我现在需要使用另一个尝试将@searchString 解析为 INT 的简单查询来联合它,如果成功,则通过查找 PK 等于解析 @searchString 的行来过滤表。

以前,我可以轻松地将 FREETEXTTABLE 结果加入它正在搜索的表中,按排名排序,但只能选择搜索的原始表的列。

现在我将文本搜索查询和以搜索字符串作为键的行的查询相结合,我不再可以从文本搜索查询访问排名。

如何保持全文搜索的排名顺序,但将查询结果放置在全文搜索结果之前查找具有主键的行(如果有结果)?

4

2 回答 2

0

在 Scott 的大量帮助下,这就是我最终得到的结果:

CREATE PROCEDURE dbo.testProcedure
(
    @searchPhrase nvarchar(500)
)

AS

DECLARE @id int
SET @id = 0;
BEGIN TRY
    SET @id = CAST(@id AS int)
END TRY
BEGIN CATCH 
END CATCH;
-- at this point, @id will be the primary key if it is only digits
-- otherwise it will default to zero (which is out of range of my identity PK)

WITH ftsTable AS (
    SELECT RANK, [KEY] FROM FREETEXTTABLE(sourceTable, *, @searchPhrase)
    UNION
    SELECT 1001, (SELECT sourceTableID FROM sourceTable WHERE sourceTableID = @id)
)   

SELECT sourceTable.*
FROM ftsTable JOIN sourceTable ON ftsTable.[KEY] = sourceTable.sourceTableID
ORDER BY ftsTable.RANK DESC
于 2009-07-30T15:46:51.237 回答
0

您是否尝试过在您的联合中添加一个常量,将您的 PK 完全匹配放在列表的顶部?我不记得自由文本 RANK 列返回什么(我认为是 0 到 1000),但是假设你只是让你的常数高于排名的顶部,这样的事情会起作用。

DECLARE @id int

IF ISNUMERIC(@myStringId) = 1
    SET @id = CAST(@myStringId AS int)
ELSE
    SET @id = 0

WITH MyFreetextCte as (SELECT   [Rank], 
                                [Key]
                       FROM     FREETEXTTABLE(...)
                       UNION
                       SELECT   1001, 
                                (SELECT MyBaseTable.PK FROM MyBaseTable WHERE PK = @id))
SELECT    *
FROM      MyFreetextCte JOIN MyBaseTable ON MyFreetextCte.[Key] = MyBaseTable.PK
ORDER BY  MyFreetextCte.Rank DESC
于 2009-07-29T19:00:30.057 回答