asp.net paging stored procedure parsing

Source: Internet
Author: User
The code is as follows Copy Code

ALTER PROCEDURE [dbo]. [Sp_sql_paging]
(
@SqlDataTable NVARCHAR (4000),--table name
@PrimaryKey NVARCHAR (4000),--primary Key name
@Fields NVARCHAR (4000),--the field to be returned
@pageSize INT,--page size
@pageIndex INT,--page number
@recordCount INT OUTPUT,--Total number of records
@strOrderBy NVARCHAR (4000),--sort
@strWhere NVARCHAR (4000)--Query conditions
)
As
BEGIN
SET NOCOUNT on
DECLARE @strSQL1 NVARCHAR (4000)--SQL statement 1
DECLARE @strSQL2 NVARCHAR (4000)--SQL statement 2
DECLARE @strSQL3 NVARCHAR (4000)--SQL statement 3

SET @strSQL1 = ' SELECT ' + @PrimaryKey + ', row_number () over (' + @strOrderBy +

' As RowNumber from ' + @SqlDataTable + ' + @strWhere

--Get the total number of records
SET @strSQL3 = ' SELECT @recordCount = COUNT (*) from ' + @SqlDataTable + ' +

@strWhere
EXEC sp_executesql
@stmt = @strSQL3,
@params = N ' @recordCount as INT OUTPUT ',
@recordCount = @recordCount OUTPUT

--Paging query
IF @pageIndex > @recordCount * 1.0/@pageSize + 1.0 OR @recordCount <= @pageSize
BEGIN
SET @pageIndex = 1
End
SET @strSQL2 = ' SELECT ' + @Fields + ' from ' + @SqlDataTable + ' WHERE ' +

@PrimaryKey + ' in (SELECT ' + @PrimaryKey + ' from (' + @strSQL1 + ') temptable WHERE

RowNumber BETWEEN ' + str ((@pageIndex-1) * @pageSize + 1) + ' + ' + str

(@pageIndex * @pageSize) + ') ' + @strOrderBy
EXEC sp_executesql @strSQL2
End

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.