0

我正在測試將數據庫從SQL Server 2005移動到2008年。我們使用CTE進行分頁。SQL Server 2008 CTE和CONTAINSTABLE語句 - 爲什麼錯誤?

當使用全文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行當前 命令發生了 嚴重錯誤。結果,如果有的話,應該丟棄 。消息0,級別20,狀態 0,行0當前命令 發生嚴重錯誤。結果,如果 任何,應該被丟棄。

有什麼建議嗎?

+0

您需要突出的代碼行(如您的SQL語句),然後按編輯器工具欄上的「代碼」按鈕(101 010)(或按Ctrl-K鍵盤上)以良好的格式和語法突出顯示這些代碼段! – 2009-11-28 16:04:29

回答

1

假設其他答案是正確的,並且潛在的問題是一個錯誤,因爲您沒有引用CONTAINSTABLE中的RANK,可能類似下面的查詢將是一種解決方法,其中「ID」是ID列在VW_GBU_Search(未測試)?

;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」條款?你能消除它嗎?

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)