SQL Server 2008 CTE 和 CONTAINSTABLE 语句 - 为什么会出现错误?

发布于 2024-08-13 00:16:34 字数 815 浏览 6 评论 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 A 当前发生严重错误 命令。如果有结果的话,应该 被丢弃。消息 0,级别 20,状态 0,第 0 行发生严重错误 当前命令。结果,如果 任何,都应该被丢弃。

有什么建议吗?

I am testing out moving our database from SQL Server 2005 to 2008. We use CTE's for paging.

When using full-text CONTAINSTABLE, the CTE will not run and generates an error.

Here's my non-working code-

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  

If I comment out the CONTAINSTABLE line, the statement executes. If I only run the SELECT statement (not the WITH), the statement executes fine.

The un-helpful error I get on this is:

Msg 0, Level 11, State 0, Line 0 A
severe error occurred on the current
command. The results, if any, should
be discarded. Msg 0, Level 20, State
0, Line 0 A severe error occurred on
the current command. The results, if
any, should be discarded.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟火散人牵绊 2024-08-20 00:16:34

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

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

Appears to be a bug. See http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=426981

Sounds like the fix should be in the next MSSQL SP.

烈酒灼喉 2024-08-20 00:16:34

假设其他答案是正确的,并且根本问题是一个错误,因为您没有从 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”条款?你能消除它吗?

Assuming the other answers are correct, and that the underlying issue is a bug, since you aren't referencing RANK from CONTAINSTABLE, perhaps a query something like the following would be a workaround, where "ID" is the ID column in VW_GBU_Search (untested)?

;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

Also, why do you have the "1=1" clause? Can you eliminate it?

爱殇璃 2024-08-20 00:16:34

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

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)

I banged my head against the wall on this problem for hours; here is a workaround:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文