SQL statement memory limit Process

Source: Internet
Author: User

Create procedure [DBO]. [up_getrecordbypage]
@ tblname varchar (255), -- table name
@ fldname varchar (255), -- Key filed name
@ pagesize Int = 10, -- page size
@ pageindex Int = 1, -- page index
@ isrecount bit = 0, -- Record count
@ ordertype bit = 0, -- ASC: 0 or Desc: 1
@ strwhere varchar (1000) = ''-- where
as

Declare @ strsql varchar (6000)
Declare @ strtmp varchar (100)
Declare @ strorder varchar (400)

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

If @ isrecount! = 0
Set @ strsql = 'select count (*) as total from ['+ @ tblname +'] '+ 'where' + @ strwhere

Exec (@ strsql)

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.