SQL Server Paging

Source: Internet
Author: User
Tags comments

Reference: SQL Paging Statements The first method: the most efficient

?
1234567891011 SELECT TOP 页大小 * FROM    (        SELECT ROW_NUMBER() OVER (ORDER BY id) AS RowNumber,* FROM table1    )   as temp  WHERE RowNumber > 页大小*(页数-1) ORDER BY id--注解:首先利用Row_number()为table1表的每一行添加一个行号,给行号这一列取名‘RowNumber‘ 在over()方法中将‘RowNumber‘做了升序排列--然后将‘RowNumber‘列 与table1表的所有列 形成一个表A--重点在where条件。假如当前页(currentPage)是第2页,每页显示10个数据(pageSzie)。那么第一页的数据就是第11-20条--所以为了显示第二页的数据,即显示第11-20条数据,那么就让RowNumber大于 10*(2-1) 即:页大小*(当前页-1)

Write the above method into a stored procedure (table name location)

?
123456789 if(exists(select* from sys.procedures where name=‘p_location_paging‘))--如果p_location_paging这个存储过程存在drop proc p_location_paging  --那么就删除这个存储过程gocreate proc p_location_paging(@pageSize int, @currentPage int)--创建存储过程,定义两个变量‘每页显示的条数‘和‘当前页‘asselecttop (@pageSize) * from (select ROW_NUMBER() over(order by locid) as rowid ,* from location )as Awhere rowid> (@pageSize)*((@currentPage)-1) ORDER BY locid


Second approach: efficiency

?
1234567891011 SELECT TOP 页大小 *  --如果每页显示10条数据,那么这里就是查询10条数据FROM table1WHERE id >  --假如当前页为第三页,那么就需要查询21-30条数据,即:id>20        (            SELECT ISNULL(MAX(id),0)  --查询子查询中最大的id            FROM                (                    SELECT TOP 页大小*(当前页-1) id FROM table1 ORDER BY id --因为当前页是第三页,每页显示十条数据。那么我将: 页大小*(当前页-1),就是获取到了在"当前页""前面"的20条数据。所以上面用max(id)查询最大的id,取到这个20,那么前面的where 条件的id>20 即取到了第三页的数据,即取21-30条数据                ) as A           )ORDER BY id

Write the above method as a stored procedure: Table name location?
12345678910 if(exists(select * from sys.procedures where name=‘p_location_paging‘))drop proc p_location_paginggocreate proc p_location_paging(@pageSize int ,@currentPage int)asselecttop (@pageSize) * from locationwhere locId>(select ISNULL(MAX(locId),0)from (select top ((@pageSize)*(@currentPage-1))locid from location  order by locId) as a)order by locId

Third approach: worst-case effect

?
12345678 SELECT TOP 页大小 *FROM table1WHERE id NOT IN --where条件语句限定要查询的数据不是子查询里面包含的数据。即查询"子查询"后面的10条数据。即当前页的数据        (           --如果当前页是第二页,每页显示10条数据,那么这里就是获取当前页前面的所有数据。           SELECT TOP 页大小*(当前页-1) id FROM table1 ORDER BY id        )ORDER BY id

 

SQL Server Paging

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.