代码之家  ›  专栏  ›  技术社区  ›  spinon

为什么我的结果不能保持一致?

  •  2
  • spinon  · 技术社区  · 14 年前

    我有以下正在处理的存储过程。我注意到,每隔5到6次刷新结果时,其中都会有新值。考虑到数据处于静态环境中,此时没有人对数据进行任何更改,我真的无法理解。有人能告诉我为什么我会看到不同的结果,即使我用完全相同的参数运行这个过程。我甚至在查询分析器中尝试过,仍然看到相同的奇怪结果。

    我正在运行SQL 2008。

    程序如下:

    ALTER PROCEDURE [dbo].[SelectSearchBy_Category]
        @userId     INT,
        @page       INT,
        @results        INT,
        @category       NVARCHAR(50),
        @searchTerm NVARCHAR(200) = NULL
    AS
    BEGIN
    
        SET NOCOUNT ON
        SET ROWCOUNT @results
    
        DECLARE @categoryId INT
    
        IF (@category IS NOT NULL) BEGIN
            SET @categoryId = ( SELECT categoryId FROM Category WHERE categoryDescription = @category )
        END
    
        DECLARE @rowEnd     INT
        DECLARE @rowStart   INT
        SET @rowEnd = (@page * @results)
        SET @rowStart = @rowEnd - @results
    
        ;WITH OrderedItems AS 
        (   
            SELECT
                i.itemId,
                title,
                i.[description],
                i.url,
                i.categoryId,
                i.ratingId,
                i.requirements,
                ISNULL(i.rating, 0) AS tating,
                ISNULL(i.raters, 0) AS raters,
                i.urlFriendlyPath,
                ROW_NUMBER() OVER
                (
                    ORDER BY i.dateAdded, (ISNULL(i.rating, 0) * ISNULL(i.raters, 0))
                ) AS RowNumber
            FROM
                [dbo].[Item] i
            LEFT JOIN
                UserItemIgnore uii ON uii.itemId = i.itemId AND uii.userId = @userId
            INNER JOIN
                ItemLanguage il ON il.itemId = i.itemId
            WHERE
                (@searchTerm IS NULL OR a.title LIKE '%' + @searchTerm + '%') AND
                i.categoryId = @categoryId AND
                il.languageId = 1 AND
                uii.itemId IS NULL
        )
    
        SELECT *
        FROM OrderedItems
        WHERE RowNumber BETWEEN @rowStart AND @rowEnd
    
    END
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   Martin Smith    14 年前

     ROW_NUMBER() OVER (ORDER BY i.dateAdded, 
                                 (ISNULL(i.rating, 0) * ISNULL(i.raters, 0)), 
                                 i.itemId)
    

    i.itemId ROW_NUMBER i.dateAdded, (ISNULL(i.rating, 0) * ISNULL(i.raters, 0))

        2
  •  2
  •   Pablo Santa Cruz    14 年前

    order by OrderedItems