ASP paging Stored Procedure for tens of millions of data

Source: Internet
Author: User

The test syntax is as follows: powered by jb51.net
Exec getrecordfrompage news, newsid, 10, 100000
News is the table name and newsid is the key field. Create an index for newsid before use. CopyCode The Code is as follows :/*
Function Name: getrecordfrompage
Function: obtains data on a specified page.
Parameter description: @ tblname indicates the name of the table containing data.
@ Fldname key field name
@ Pagesize number of records per page
@ Pageindex: page number to be obtained
@ Ordertype: Sorting type. Values: 0 (ascending) and 1 (descending ).
@ Strwhere query condition (Note: Do not add where)
Prepared by: Tie Quan
Mail: sunjianhua_kki@sina.com
Created on:
Modification time:
*/
Create procedure getrecordfrompage
@ Tblname varchar (255), -- table name
@ Fldname varchar (255), -- field name
@ Pagesize Int = 10, -- page size
@ Pageindex Int = 1, -- page number
@ Ordertype bit = 0, -- set the sorting type. If the value is not 0, the sorting type is descending.
@ Strwhere varchar (2000) = ''-- Query condition (Note: Do not add where)
As

Declare @ strsql varchar (6000) -- subject sentence
Declare @ strtmp varchar (1000) -- Temporary Variable
Declare @ strorder varchar (500) -- sort type

If @ ordertype! = 0
Begin
Set @ strtmp = '<(select min'
Set @ strorder = 'order by ['+ @ fldname +'] desc'
End
Else
Begin
Set @ strtmp = '> (select Max'
Set @ strorder = 'order by ['+ @ fldname +'] ASC'
End

Set @ strsql = 'select top '+ STR (@ pagesize) +' * from ['
+ @ Tblname + '] Where [' + @ fldname + ']' + @ strtmp + '(['
+ @ Fldname + ']) from (select top' + STR (@ PageIndex-1) * @ pagesize) + '['
+ @ Fldname + '] from [' + @ tblname + ']' + @ strorder + ') as tbltmp )'
+ @ Strorder

If @ strwhere! =''
Set @ strsql = 'select top '+ STR (@ pagesize) +' * from ['
+ @ Tblname + '] Where [' + @ fldname + ']' + @ strtmp + '(['
+ @ Fldname + ']) from (select top' + STR (@ PageIndex-1) * @ pagesize) + '['
+ @ Fldname + '] from [' + @ tblname + '] Where' + @ strwhere +''
+ @ Strorder + ') as tbltmp) and' + @ strwhere + ''+ @ strorder

If @ pageindex = 1
Begin
Set @ strtmp =''
If @ strwhere! =''
Set @ strtmp = 'where ('+ @ strwhere + ')'

Set @ strsql = 'select top '+ STR (@ pagesize) +' * from ['
+ @ Tblname + ']' + @ strtmp + ''+ @ strorder
End

Exec (@ strsql)

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.