如何检查是否已达到最后一条记录

时间:2019-06-14 11:17:02

标签: sql-server

我有以下代码,它从tblItems返回可能的4行。但是,如果任何选定的行恰好是tblItems中的最后一行,如何将@lastBox返回为“ Y”

我设置了DECLARE @lastBox varchar(1),我想返回Y或N来表示它是否是最后一个盒子。不知道从那里去哪里。

ALTER PROCEDURE [dbo].[SelectBoxes]
@bootSaleDate   DATE,
@itemcategory   Int,
@page  Int

AS
BEGIN
   SET NOCOUNT ON;

   select * from (SELECT ROW_NUMBER() OVER (ORDER BY dateAdded desc) as [RowNo],  bootSaleDate,  '<a href="https://www.myUrl.co.uk/video.aspx?oid='  + CAST(ISNULL([fileID],'') AS NVARCHAR(5)) + '"><br /><image width="140" height="200" src="catalog/thumbnails/' + convertedFilename + '" /></a>' as strText, fileID, [buyPrice] as buyPrice

   FROM [tblItems] WHERE sold='n' AND itemCategory=@itemCategory)t

   where RowNo between (@page*4)-3 AND (@page*4)

   DECLARE @lastBox   varchar(1)

END

1 个答案:

答案 0 :(得分:0)

将行号与计数进行比较:

SELECT *,
    CASE WHEN RowNo < cnt THEN 'N' ELSE 'Y' END AS box
FROM
(
    SELECT ROW_NUMBER() OVER (ORDER BY dateAdded desc) as [RowNo],
        COUNT(*) OVER () cnt,
        bootSaleDate,  '<a href="https://www.myUrl.co.uk/video.aspx?oid='  + CAST(ISNULL([fileID],'') AS NVARCHAR(5)) + '"><br /><image width="140" height="200" src="catalog/thumbnails/' + convertedFilename + '" /></a>' as strText, fileID, [buyPrice] as buyPrice
    FROM [tblItems]
    WHERE sold = 'n' AND itemCategory = @itemCategory
) t
WHERE RowNo BETWEEN (@page*4)-3 AND (@page*4)