SQL Paging Stored Procedures

Source: Internet
Author: User

ALTER procedure [dbo]. [Fenye]
@pagesize int,--Display quantity per page
@pageCurrent int,--current page
@tablename varchar (20),--table name
@field varchar (20),--displayed column name (eg:id,name)
@where varchar (20),--Filter condition (eg:name NOT NULL)
@orderBy varchar (20),--Sorted column name (eg:id or ID desc)
@count int output--Returns the total number of pages, 0 does not return 1 bits

As
Begin
declare @strSql nvarchar (200)
DECLARE @starNum int
DECLARE @endNum int
Set @starNum = (@pageCurrent-1) * @pagesize
Set @endNum [email protected] * @pagesize
--declare @timediff datetime

--set nocount ON--no count is returned (indicates the number of rows affected by the Transact-SQL statement).
--select @timediff =getdate ()--record time

If @count!=0
begin
If @where = '
Set @strSql = ' Select @count =count (*) from ' [email protected]
else
Set @strSql = ' Select @count =count (*) from ' [email protected]+ ' where ' [email protected]
End
EXEC sp_executesql @strSql, N ' @count int output, @tablename varchar, @where varchar, @count output, @tablename, @ where


If @pageCurrent =1
If @where = '
Set @strSql = ' Select Top ' +cast (@pagesize as varchar) + ' [EMAIL PR otected]+ ' from ' [email protected]+ ' ORDER by ' [email protected]+ '
Else
Set @strSql = ' Select Top ' +cas T (@pagesize as varchar) + ' [email protected]+ ' from ' [email protected]+ ' where ' [email protected]+ ' Order BY ' [email protected]+ '
Else
if @where! = '
Set @strSql = ' SELECT ' [email protected]+ ' from ( Select ' [email protected]+ ', Row_number () over (order by ' [email protected]+ ') rn from ' [email protected ]+ ' where ' [email protected]+ ') A Where rn<= ' +convert (varchar, @endNum) + ' and rn> ' +cast (@starNum as varchar) + '
Else
Set @strSql = ' SELECT ' [email protected]+ ' from (select ' [email protected]+ '], row_number () over (Order by ' [email protected]+ ') rn from ' [email protected]+ ') A Where rn<= ' +convert (varchar, @endNum) + ' and Rn> ' +cast (@starNum as varchar) + '

EXEC (@strSql)

--select DateDiff (MS, @timediff, GETDATE ()) as time-consuming
--set nocount Off--Returns the count (default is off).
End

DECLARE @count int
Set @count =1
exec fenye 3,3,CJ, ' * ', ' Fenshu is not null ', ' ID ', @count output
Select @count

SQL Paging Stored Procedures

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.