2009-11-22 55 views
0

我有這個SQL查詢,我試圖使用CONTAINS搜索標題字段。如何在SQL Server 2008中使用CONTAINS和inline查詢?

但我得到這個錯誤。

「Can not use CONTAINS or FREETEXT predicate on column'Title',because it's not full-text indexed。」

標題表已被編入索引,CONTAINS可以通過簡單搜索正常工作。

有誰知道我在做什麼錯?內聯查詢不支持CONTAIN查詢嗎?

此查詢是在SQL Server 2008跑去

SELECT pi.PublisherGUID, pi.Publisher, pi.TitleGUID, pi.Title, 
    pi.YearsPublished, pi.FrontImage, pi.IssueGUID, pi.IssueNumber, 
    pi.IssueVariation, pi.IssueNotes, pi.CoverDate, pi.IsForSale 
    FROM (
    SELECT ROW_NUMBER() OVER(ORDER BY PublicIssues.Title,PublicIssues.IssueNumber) AS RowNum, 
     PublicIssues.PublisherGUID, PublicIssues.Publisher, 
     PublicIssues.TitleGUID, PublicIssues.Title, 
     PublicIssues.YearsPublished, PublicIssues.FrontImage, 
     PublicIssues.IssueGUID, PublicIssues.IssueNumber, 
     PublicIssues.IssueVariation, PublicIssues.IssueNotes, 
     PublicIssues.CoverDate, PublicIssues.IsForSale 
     FROM (SELECT dbo.tblTitles.PublisherGUID, dbo.tblPublishers.Name AS Publisher, 
      dbo.tblTitles.TitleGUID, dbo.tblTitles.Title, 
      dbo.tblTitles.YearsPublished, dbo.tblIssues.IssueGUID, 
      dbo.tblIssues.IssueNumber, dbo.tblIssues.IssueVariation, 
      dbo.tblIssues.IssueNotes, dbo.tblIssues.CoverDate, 
      dbo.tblStockIssueImages.FrontImage, 
      ci_owner.IssueForSale(dbo.tblIssues.IssueGUID) AS IsForSale 
      FROM dbo.tblStockIssueImages RIGHT OUTER JOIN 
       dbo.tblIssues ON 
       dbo.tblStockIssueImages.StockIssueImageGUID = dbo.tblIssues.StockIssueImageGUID 
       LEFT OUTER JOIN 
       dbo.tblTitles INNER JOIN 
       dbo.tblPublishers ON dbo.tblTitles.PublisherGUID = dbo.tblPublishers.PublisherGUID 
       ON dbo.tblIssues.TitleGUID = dbo.tblTitles.TitleGUID 
      ) 
    AS PublicIssues 
    WHERE 1=1 AND CONTAINS(Title,@xTitle) 
) AS pi 
WHERE RowNum BETWEEN (@xPageNum - 1) * @xPageSize + 1 AND 
@xPageNum * @xPageSize ORDER BY pi.Title 

回答

2

事實上,在PublicIssues的背景下,標題不是建立全文索引。

它被編入表tblTitles中。

我認爲可能在定義PublicIssues的表達式中移動CONTAINS謂詞。像下面這樣。然而,我懷疑(有1 = 1的提示​​)這個想法是有各種其他標準,並且將它們全部「內部」可能是不可行的。它[顯然]是動態SQL,通過將搜索條件放在適當的兩個位置中的一個位置來創建查詢可能是可行的。

FROM (SELECT dbo.tblTitles.PublisherGUID, dbo.tblPublishers.Name AS Publisher, 
     dbo.tblTitles.TitleGUID, dbo.tblTitles.Title, 
      dbo.tblTitles.YearsPublished, dbo.tblIssues.IssueGUID, 
      dbo.tblIssues.IssueNumber, dbo.tblIssues.IssueVariation, 
      dbo.tblIssues.IssueNotes, dbo.tblIssues.CoverDate, 
     dbo.tblStockIssueImages.FrontImage, 
      ci_owner.IssueForSale(dbo.tblIssues.IssueGUID) AS IsForSale 
     FROM dbo.tblStockIssueImages RIGHT OUTER JOIN 
        dbo.tblIssues ON 
        dbo.tblStockIssueImages.StockIssueImageGUID = dbo.tblIssues.StockIssueImageGUID 
        LEFT OUTER JOIN 
        dbo.tblTitles INNER JOIN 
        dbo.tblPublishers ON dbo.tblTitles.PublisherGUID = dbo.tblPublishers.PublisherGUID 
        ON dbo.tblIssues.TitleGUID = dbo.tblTitles.TitleGUID 

      WHERE CONTAINS(Title,@xTitle) --- this lined moved 
      ) 
AS PublicIssues 
+0

爲了解決這個問題,可能你只需要創建一個整個子查詢作爲一個臨時表,並創建在該臨時表中的全文索引,然後從選擇? – SqlRyan 2009-11-22 03:53:08

+0

你可能只是:a)你將不得不打破3中的查詢(選擇/創建臨時表,FT索引,從中搜索),更重要的是,創建FT索引可能需要比預期更長的時間。 – mjv 2009-11-22 03:59:39

+0

我猜SQLServer是不夠聰明,知道該字段在子查詢中使用時索引。你認爲這是因爲你必須選擇你想要索引的字段。 – 2009-11-23 01:23:27