Switch: SQL server data paging storage process

Source: Internet
Author: User
  Normally SQL When the number of data items in a query statement is small, the speed will be slower, because it takes a lot of time to transmit a large amount of data over the network. If you query data by PAGE, only useful data is returned. After you exclude unnecessary data, the transmission speed is much faster. SQL Paging stored procedure. This process is based on SQL Built-in Northwind The database is written as an example and can be changed as needed.

Create Procedure [Getcustomersdatapage]
@ Pageindex Int , -- Page number
@ Pagesize Int , -- Page number
@ Recordcount Int Out , -- Number of records
@ Pagecount Int Out -- Page number
As
Select @ Recordcount = Count (*) From MERs
Set @ Pagecount = Ceiling (@ Recordcount * 1.0/@ pagesize)

Declare@ SqlstrNvarchar(1000)

If @ Pageindex = 0 Or @ Pagecount <= 1
Set @ Sqlstr = 'Select top' + Str (@ Pagesize) + 'Mermerid, companyName, address, phone from MERs order by mermerid DESC'

Else If @ Pageindex = @ pagecount-1
Set @ Sqlstr = 'Select * from (select top' + Str (@ Recordcount-@ pagesize * @ pageindex) + 'Mermerid, companyName, address, phone from MERs order by customerid ASC) temptable order by mermerid DESC'

Else
Set @ Sqlstr = 'Select top' + Str (@ Pagesize) + '* From (select top' + Str (@ Recordcount-@ pagesize * @ pageindex) + 'Mermerid, companyName, address, phone from MERs order by customerid ASC) temptable order by mermerid DESC'

Exec(@ Sqlstr)

Go

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.