这是我的 sql server 2008 存储过程。
ALTER PROCEDURE [dbo].[GetSharedSmoothies]
@Page INT ,
@Status INT ,
@ItemPerPage INT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @X INT
DECLARE @Y INT
SET @X = ( @Page - 1 ) * @ItemPerPage
SET @Y = @Page * @ItemPerPage
SELECT *
FROM ( SELECT S.* ,
U.Avatar ,
U.Displayname ,
( SELECT COUNT(Id)
FROM Vote
WHERE Vote.SmoothieId = S.Id
) AS Votes ,
ROW_NUMBER() OVER ( ORDER BY S.Id ) rownum
FROM dbo.Smoothie AS S
INNER JOIN dbo.[User] AS U ON S.UserId = U.Id
WHERE S.IsPublic = 1
AND S.Status = 3
AND S.UserId > 0
-- ORDER BY S.CreatedDate DESC
) seq
WHERE seq.rownum BETWEEN @X AND @Y
ORDER BY seq.rownum
END
在我的代码中,你会看到我注释掉了订单
-- ORDER BY S.CreatedDate DESC
因为 order by 在子查询中不起作用。我需要在顶部显示最新的。有没有办法可以在我的代码中使用 order by?