A paging storage process with good scalability

Source: Internet
Author: User

This is a frequently used paging storage process. I hope you can give me some advice.
Copy codeThe Code is as follows:
USE [a6756475746]
GO
/***** Object: StoredProcedure [dbo]. [tbl_order_SearchWhereAndPage] Script Date: 11/01/2011 09:37:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Alter procedure [dbo]. [tbl_order_SearchWhereAndPage]
@ AllCount int OUTPUT,
@ PageIndex int,
@ PageSize int,
@ MinDate datetime,
@ MaxDate datetime
AS
Begin
DECLARE @ PageLower int
Set @ PageLower = @ PageSize * @ PageIndex
DECLARE @ PageUpper int
Set @ PageUpper = @ PageLower + @ PageSize-1

DECLARE @ SearchSQL nvarchar (4000)
Set @ SearchSQL = 'select *, (ROW_NUMBER () OVER (order by [ID] DESC)-1) AS RowNumber FROM tbl_order WHERE (1 = 1 )'
DECLARE @ SearchSQLCount nvarchar (4000)
Set @ SearchSQLCount = 'select @ count = Count (*) FROM tbl_order WHERE (1 = 1 )'
Declare @ Result [varchar] (5000)
Set @ Result =''

If @ minDate> convert (datetime, '2017-1-2 ')
Begin
Set @ Result = @ Result + 'and oDeliveryDate> = ''' + convert (varchar (20), @ minDate) + ''''
End
If @ maxDate> convert (datetime, '2017-1-2 ')
Begin
Set @ Result = @ Result + 'and oDeliveryDate <= ''' + convert (varchar (20), dateadd (dd, 1, @ maxDate) + ''''
End
Set @ SearchSQLCount = @ SearchSQLCount + @ Result
Set @ SearchSQL = @ SearchSQL + @ Result

SET @ SearchSQL = 'WITH t AS (' + @ SearchSQL + ')
SELECT * FROM t
WHERE [RowNumber] between' + convert (varchar (50), @ PageLower) + 'AND' + convert (varchar (50), @ PageUpper) +'
Order by RowNumber'

Exec (@ SearchSQL)

Exec sp_executesql @ SearchSQLCount, n' @ count as int out', @ AllCount out

Print @ SearchSQL

Print @ AllCount
End
Exec (@ Result)

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.