存储过程中的分页顺序

Paging order in stored procedure

我写了一个存储过程,return来自数据库的业务详细信息。页面大小为10。我的数据库中也有支付/未支付业务,由布尔列定义(IsPaid true/false)

我想在第一页(或前 10 页)只显示付费业务。然后在最后几页是未付费的业务。

在我的例子中,使用下面的这个存储过程。每页的分页顺序 returns。这意味着如果在第一页中没有付费业务,它将 return 返回。

如何将每个页面的顺序更改为所有 select

ALTER PROCEDURE dbo.GetBusUsingPaginNoTown
    @PageNo int,
    @NoOfRecord int,
    @Id int,
    @TotalRecord int output
AS
    select @TotalRecord = count(*) 
    from BusinessFullData

    select * 
    from 
        (select 
             Row_number() over (order by b.Id ASC) as RowNo,
             b.Ispaid, b.Id, b.name, b.Addess, b.DefaultImage,
             t.TownName, c1.CategoryName AS CatN1, 
             c2.CategoryName AS CatN2, c3.CategoryName AS CatN3 
         from 
             BusinessFullData b 
         left join
             Towns t on b.Town = t.Id 
         left join 
             Categories c1 ON b.cat1 = c1.Id 
         left join
             Categories c2 ON b.cat2 = c2.Id 
         left join
             Categories c3 ON b.cat3 = c3.Id 
         where  
              ((b.IsVisable = 1) 
                AND ((b.Cat1 = @Id) OR (b.Cat2 = @Id) OR (b.Cat3 = @Id)
              )) 
        ) as Tab
  where 
      Tab.RowNo between ((@PageNo - 1) * @NoOfRecord) + 1  
                    and (@PageNo * @NoOfRecord) 
  order by 
      IsPaid desc

  return 

over partition 子句应包含所需的顺序。在你的情况下

Row_number() over (order by b.Id ASC) as RowNo,

应替换为

Row_number() over (order by b.isPaid ASC) as RowNo,

希望这对您有所帮助。这就是我目前所能看到的,除非您以更清楚的方式描述您的问题...