0

我正在测试将我们的数据库从 SQL Server 2005 迁移到 2008。我们使用 CTE 进行分页。

使用全文 CONTAINSTABLE 时,CTE 将不会运行并生成错误。

这是我的非工作代码-

WITH results  AS (  
 SELECT ROW_NUMBER() over (ORDER BY  GBU.CreateDate DESC ) as rowNum,             
 GBU.UserID,  
 NULL AS DistanceInMiles   
 FROM User GBU WITH (NOLOCK)  
 WHERE 1=1   
 AND GBU.CountryCode IN (SELECT [Value] FROM fn_Split('USA',','))   
 AND GBU.UserID IN (SELECT [KEY] FROM CONTAINSTABLE(VW_GBU_Search, *, 'COMPASS'))  
 )
SELECT * from results  
WHERE rowNum BETWEEN 0 and 25  

如果我注释掉 CONTAINSTABLE 行,语句就会执行。如果我只运行 SELECT 语句(而不是 WITH),则该语句执行得很好。

我得到的无用错误是:

消息 0,级别 11,状态 0,行 0 当前命令发生严重错误。结果,如果有的话,应该丢弃。Msg 0, Level 20, State 0, Line 0 当前命令发生严重错误。结果,如果有的话,应该丢弃。

有什么建议么?

4

3 回答 3

1

假设其他答案是正确的,并且根本问题是一个错误,因为您没有从 CONTAINSTABLE 引用 RANK,也许类似以下的查询将是一种解决方法,其中“ID”是 VW_GBU_Search 中的 ID 列(未经测试)?

;WITH results AS (  
 SELECT ROW_NUMBER() OVER (ORDER BY  GBU.CreateDate DESC ) AS rowNum,                     
   GBU.UserID,  
   NULL AS DistanceInMiles   
   FROM User GBU WITH (NOLOCK)  
   WHERE 1=1   
   AND GBU.CountryCode IN (SELECT [Value] FROM fn_Split('USA',','))   
   AND GBU.UserID IN (SELECT ID FROM VW_GBU_Search WHERE CONTAINS(*, 'COMPASS')) 
)
SELECT * FROM results  
  WHERE rowNum BETWEEN 0 AND 25

另外,为什么你有“1=1”条款?你能消除它吗?

于 2009-11-29T04:03:41.203 回答
1

似乎是一个错误。请参阅http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=426981

听起来修复应该在下一个 MSSQL SP 中。

于 2009-11-28T15:00:00.450 回答
0

在这个问题上,我把头撞在墙上好几个小时。这是一种解决方法:

ASSUME: A table in database called 
        Items ( ItemId int PK, Content varchar(MAX) ), 
        which has a fulltext index already applied.

GO
CREATE FUNCTION udf_SearchItemsTable(@FreeText)
RETURNS @SearchHits
TABLE(
   Relevance int,
   ItemId int,
   Content varchar(MAX)
)
AS 
BEGIN
   INSERT @SearchHits
   SELECT Results.[Rank] AS Relevance
         ,Items.ItemId AS ItemId
         ,Items.Content AS Content
   FROM SearchableItems AS Items INNER JOIN 
         CONTAINSTABLE(SearchableItems, *, @FreeText) AS Results
           Results.[Key] = Items.Id
   RETURN
END
GO
...
GO
CREATE FUNCTION udf_SearchItems( @SearchText, @StartRowNum, @MaxRows)
RETURNS @SortedItems
TABLE (
   ItemId int,
   Content varchar(MAX)
)
AS
BEGIN
    WITH Matches AS
    (
         SELECT 
            ROW_NUMBER() OVER (ORDER BY Hits.Relevance DESC) AS RowNum
           ,Hits.*
        FROM ( udf_SearchItemsTable(@SearchText) ) AS Hits
    )
    SELECT
        ItemId, Content
    FROM
        Matches
    WHERE
        Matches.RowNum BETWEEN @StartRowNum 
    AND @StartRowNum + @MaxRows
    ;
    RETURN
END
GO   


select * from udf_SearchItems('some free text stuff', 10, 20)
于 2010-07-23T08:24:49.580 回答