Summary of common paging methods for SQL Server

Source: Internet
Author: User

Summary of common paging methods for SQL Server

The following example summarizes the common paging methods for SQL Server databases, which are used only for learning reference

A, using RowNumber and between and combination paging:
/********** using RowNumber and between and combination paging **********/create PROC proc_fuzzysearchandpaging@pageindex int,           -- Page index @pagesize int,            --page size @searchkey Nvarchar (Ten),  --query keyword @totalcount int OutPut    --Total number of data bars as  BEGIN       --Query the current page data       select * FROM (           select *,[no]=row_number () over (ORDER by s.s_id DESC) from Stuinfo s                  WHERE s.s_name Like ('% ' [email protected]+ '% ')       ) T        WHERE T.[no] between @pageSize * (@pageIndex-1) +1 and @pageIndex * @pageSize C11/>order by t.s_id DESC              --Total number of data bars       SELECT @TotalCount = count (*) from Stuinfo s WHERE s.s_name like ('% ' [email pro tected]+ '% ')  Endgo

B, using the TOP and not in combination paging:
/********** using TOP and not in combination paging **********/create PROC proc_fuzzysearchandpaging2@pageindex int,             --current page index @pagesize in T,              --number of data bars per page @fuzzykey Nvarchar,     --fuzzy matching keyword @count int OUTPUT           --Total number of data bars (used to determine how many pages to divide) as  BEGIN      SELECT TOP (@PageSize) * from Stuinfo s  WHERE s.s_name like ('% ' [email protected]+ '% ') and s.s_id not in  (      Selec T TOP ((@PageIndex-1) * @PageSize) s.s_id from Stuinfo s             WHERE s.s_name like ('% ' [email protected]+ '% ')             ORDER by s.s_id ASC  )  ORDER by s.s_id ASC       --Total number of data bars     SELECT @Count =count (*) from Stuinfo s WHERE s.s_name like ('% ' [email protected]+ '% ')   Endgo

If so, a better way to get out and share!


Extensions: When paging, you can provide query speed with full use of temporal tables and with as statements

Example with AS statement:
DECLARE @SearchKey Nvarchar  --query keyword with t as (      SELECT * from Stuinfo s             WHERE s.s_name like ('% ' [email prote cted]+ '% '))


Example of a temporary table statement:

DECLARE @SearchKey Nvarchar  --Query keyword SELECT * into #temp2 from (       select * from Stuinfo s WHERE s.s_name like ('% ') [Email protected]+ '% ')) u
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.