zoukankan      html  css  js  c++  java
  • SqlServer分页查询语句

    语句如下:

    declare @page int
    declare @size int
    
    set @page=2
    set @size=20 
    
    --第一种,优点,RowId可以作为行数,只扫描一次表数据,逻辑读取两次
    SET STATISTICS PROFILE ON 
    SET STATISTICS IO ON 
    SET STATISTICS TIME ON 
     /*--你的SQL脚本开始*/
    select * from (select top (@page*@size) row_number() over(order by Id) RowId,* from T1 order by RowId) T where RowId between (@page*@size-@size+1) and (@page*@size)
    --between 结果会包含边界值,所以需要+1
    /*--你的SQL脚本结束*/ SET STATISTICS PROFILE OFF SET STATISTICS IO OFF SET STATISTICS TIME OFF --第二种 扫描两次表数据,逻辑读取四次 SET STATISTICS PROFILE ON SET STATISTICS IO ON SET STATISTICS TIME ON /*--你的SQL脚本开始*/ select top (@size) * from T1 where Id not in(select top (@page*@size-@size) ID from T1 order by Id)order by Id /*--你的SQL脚本结束*/ SET STATISTICS PROFILE OFF SET STATISTICS IO OFF SET STATISTICS TIME OFF

    结果如下:

    推荐使用第一种方式 分页

  • 相关阅读:
    Log4j2 配置
    Spring + SpringMVC配置
    Tomcat 动态数据库连接池
    MySQL数据库备份命令
    一条insert语句插入数据库
    tomcat 性能优化
    linux RPM manager
    mysql 多主
    ceph学习
    python常用程序算法
  • 原文地址:https://www.cnblogs.com/Med1tator/p/9244416.html
Copyright © 2011-2022 走看看