SQL Stored Procedure Paging

Source: Internet
Author: User

ALTER PROCEDURE [dbo]. [BrokerToLenderDataShow2]
@Where VARCHAR (200),--the query condition does not contain ' Where ' characters, such as Id>10 and Len (userid) >9
[Email protected] VARCHAR (200),--sort does not contain ' order by ' characters, such as ID Asc,userid desc, you must specify ASC or DESC
-Note that when the @sorttype=3 is in effect, remember to add the primary key at the end, otherwise it will make you more depressed
[Email protected] INT,--collation 1: Positive order ASC 2: Reverse DESC 3: Multi-column Sorting method
[Email protected] INT,--Total record 0: Total record returned
@PageSize INT,--the number of records output per page
@PageIndex INT--current page, starting from 1
[Email protected] INT OUTPUT,--Remember to return the total record
[Email protected] INT OUTPUT--Returns the total number of pages

As
BEGIN
DECLARE @Sql VARCHAR, @Sql2 NVARCHAR (+), @sql3 NVARCHAR (+), @BetweenStart int, @BetweenEnd int


Set @BetweenStart = convert (int, @PageSize * @PageIndex) [email protected]+1
Set @BetweenEnd =convert (int, @PageSize * @PageIndex)

Set @Sql3 = ' select * ' From (select Row_number () over (ORDER by b.id) RowNumber,
* from loanee b where 1=1 ' [email protected]+ '
) T
where t.rownumber between ' + LTrim (str (@BetweenStart)) + ' and ' + LTrim (str (@BetweenEnd));
Print @sql3

EXEC (@sql3)

END

-----------------------------------------------------------------------------------

--No conditional call
exec [BrokerToLenderDataShow2] ", 10,1

--a call with a bar
exec [BrokerToLenderDataShow2] ' and id>10 ', 10,1

SQL Stored Procedure Paging

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.