[SQL study notes] paging query

See the following code for details:

DECLARE @PageIndex INT --当前页数
DECLARE @PageSize INT --每页行数

SET @PageIndex=3 --设置当前为第3页
SET @PageSize=7 --设置每页7行

--进行分页查询
SELECT
	*
FROM TABLE_1 T1
ORDER BY T1.AID --以AID作为依据进行升序排序
OFFSET ((@PageIndex-1)*@PageSize) ROWS --将之前所有页(即@PageIndex减一)的所有行(即乘以每页行数)进行移除
FETCH NEXT (@PageSize) ROW ONLY --显示剩余部分的@PageSize行数据

Note: You can also understand @PageIndex as the previous page. In this case, you do not need to perform a minus one operation in the removed part of the code (mainly see how the parameters of the front and back sides agree on the meaning of the parameters)

Published 23 original articles · Like1 · Visits 20,000+

Guess you like

Origin blog.csdn.net/shenjie_xsj/article/details/105114239