Get the App
SLTechnology News&Howtos  ›  Database  › 

How to operate MSSQL query data paging

Shulou Source: shulou.com Published: 2022-06-01 03:19:31 10月02日 Update

This article is to share with you about how to operate MSSQL query data paging. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.

Method 1: use select top with not in (or not exists) to filter out the data on page 1 when querying page n. The example assumes that the number of queries per page is 5, and the data on page 3 is queried.

Select Top 5 UserCode,UserName from userInfo where UserCode not in (select top ((3-1) * 5) UserCode from UserInfo order by UserCode asc) order by UserCode asc

The first 15 rows of data

The data on page three

Note that order by must use the same columns and arrangement when querying

Method 2: using the Row_Number () built-in function, first add a column of ID to the query table, and then query the pages of the between.. and... is very simple.

Select UserCode,UserName,PassWord From

(Select UserCode,UserName,PassWord,Rn=Row_Number () OVER (order by UserCode desc) From UserInfo) AS T

Where t.Rn between (3-1) * 5 and 3

Of course, in the actual application, the number of records per page can be replaced by parameters on which page of the query.

Thank you for reading! On how to operate MSSQL query data paging to share here, I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.

Tags: Query data content quantity method time more good practical same function parameter actual article see knowledge example article previous reference Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL Microsoft NVidia OPPO Reno