Sqlserver 不同分页写法

第一种 offset  x rows fetch next x rows方式(SQL2012以上的版本才支持:推荐使用,效率最高 )

语法:offset 跳过 rows

   fetch  取 rows only

   例:跳过30条,取10条

select * from [CLD].[dbo].[Dim_SalesStructure_Month] order by id offset 30 rows fetch next 10 rows only

第二种 row_number()

  先根据id排序再分页

select * from(
    select *,row_number() over(order by id) num from  [CLD].[dbo].[Dim_SalesStructure_Month]) A
    where A.num between 31 and 40

第三种 not in

select top 10 * from [CLD].[dbo].[Dim_SalesStructure_Month] where id not in(
    SELECT top 30 ID
        FROM [CLD].[dbo].[Dim_SalesStructure_Month])

第四种 not exists

select top 10 * from [CLD].[dbo].[Dim_SalesStructure_Month] a where  not exists(
    select 1 from (SELECT top 30 ID
                        FROM [CLD].[dbo].[Dim_SalesStructure_Month] order by id)a1 where a1.ID=a.ID)

第五种 取前30条 id大于top30最大id

select top 10 * from [CLD].[dbo].[Dim_SalesStructure_Month] where id >(
 select max(ID) from  (select top 30 id from [CLD].[dbo].[Dim_SalesStructure_Month]) A)
原文地址:https://www.cnblogs.com/guozhaoxin/p/14858734.html