SQL Server 2008 Paging stored procedures

Source: Internet
Author: User

In use, already do not know the source, if there is infringement please tell me, I immediately deleted.

 Use [Activaonlinesupport]GO/** * * * object:storedprocedure [dbo].    [Proce_pagechange] Script date:08/13/2014 13:45:17 * * * * **/SETAnsi_nulls onGOSETQuoted_identifier onGOALTER PROCEDURE [dbo].[Proce_pagechange]( @TableName varchar( -),--Table name @ReFieldsStr varchar( $)= '*',--Field name (All fields are *) @OrderString varchar( $),--sort fields (must! support multiple fields without order by) @WhereString varchar( -)=N"',--conditional statements (do not add where) @PageSize int,--how many records per page @PageIndex int = 1,--Specify the current page @TotalRecord intOutput--returns the total number of records) as  BEGIN         --processing start and end points    Declare @StartRecord int; Declare @EndRecord int; Declare @TotalCountSql nvarchar( -); Declare @SqlString nvarchar( -); Set @StartRecord =(@PageIndex-1)*@PageSize + 1    Set @EndRecord = @StartRecord + @PageSize - 1     SET @TotalCountSql=N'Select @TotalRecord = count (*) from' + @TableName;--Total Record Count statement    SET @SqlString =N'(select Row_number () over (order by'+ @OrderString +') as RowId,'+@ReFieldsStr+' from'+ @TableName;--Query Statements    --    IF(@WhereString!= "' or @WhereString!=NULL)        BEGIN            SET @TotalCountSql=@TotalCountSql + 'where'+ @WhereString; SET @SqlString =@SqlString+ 'where'+ @WhereString; END    --The first execution gets    --IF (@TotalRecord is null)    --BEGIN           EXECsp_executesql@totalCountSqlN'@TotalRecord int out',@TotalRecordOutput--returns the total number of records    --END    ----execution of the subject sentence    Set @SqlString ='SELECT * from' + @SqlString + ') as T where rowId between' + LTrim(Str(@StartRecord))+ ' and' +  LTrim(Str(@EndRecord)); Exec(@SqlString)    END

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.