SQL Paging Stored Procedures

Source: Internet
Author: User
Tags rowcount

Use [DBName]
GO
/****** object:storedprocedure [dbo]. [Up_getrecordbypage] Script date:12/05/2012 16:17:11 ******/
SET ANSI_NULLS on
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo]. [Up_getrecordbypage]
@tblName varchar (255),--table name
@fldName varchar (255),--primary key field name
@PageSize int = 10,--page size
@PageIndex int = 1,--page
@RowCount int output,--Returns the total number of records
@OrderType bit = 0,--set sort type, not 0 value descending
@strWhere varchar (1000) = "--Query criteria (note: Do not add where)
As

DECLARE @strSQL varchar (6000)--the subject sentence
DECLARE @strTmp varchar (100)--Temporary variable
DECLARE @strOrder varchar (400)--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

DECLARE @sql NVARCHAR (400)

Set @sql = N ' Select @RowCount =count (*) from ' [Email protected]+ ' WHERE ' + @strWhere
EXEC sp_executesql @sql, N ' @RowCount int out ', @RowCount out

EXEC (@strSQL)

GO

The second type:


if exists (SELECT * from sys.objects where name= ' proc_getrecordbypages ')
drop proc Proc_getrecordbypages
Go
CREATE PROCEDURE Proc_getrecordbypages
@PageIndex Int=1,
@PageSize int=10,
@PageCount int=1 Output
As
declare @StartIndex int, @EndIndex int
Set @StartIndex = (@PageIndex-1) * @PageSize +1
Set @[email protected] * @PageSize
SELECT * FROM
(
Select Row_number () over (order by p_order ASC) as sno,* from App_product
) as Tb1
Where Sno between @StartIndex and @EndIndex
Set @PageCount = (select COUNT (1) from app_product)
Go

DECLARE @PageCount int
exec proc_getrecordbypages 1,20, @PageCount output
Select @PageCount

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.