SQL Server paging stored procedures

Source: Internet
Author: User

/********************************************************************************** function:pagedproc                                                     * * Description: * * Sql2005 paging stored procedure                                                  * * Finish DateTime: * * 2009/1/3                                                              **    Example:                                                                      **    Web_pageview @Tablename = ' Table1 ', @Returnfields = ' * ', **            @PageSize = 2, @ PageIndex = 1, @Where = ',                     **            @OrderBy =n ' ORDER by id Desc '                                          **********************************************************************************/
IF EXISTS (SELECT * from DBO. SYSOBJECTS WHERE ID = object_id (N ' [dbo].[ Pagedproc] ') and OBJECTPROPERTY (ID, N ' isprocedure ') = 1) DROP PROCEDURE [dbo]. [Pagedproc] GO
SET QUOTED_IDENTIFIER Ongo
SET ANSI_NULLS on GO

CREATE PROCEDURE dbo. pagedproc     @TableName       nvarchar (,  )         --table name      @ReturnFields     NVARCHAR (+) = ' * ',    --need to return columns      @PageSize         int = 10,               - -Number of records per page      @PageIndex       int = 1,                 --Current Page      @Where           nvarchar (+) = ",     --query criteria      @OrderBy         nvarchar (,  )        --sort field names are best for unique primary keys         @PageCount       int output,              --page Count      @RecordCount     int output        --Total Records
With encryption as
--Set NOCOUNT on
--Variable definition declare @TotalRecord intdeclare @TotalPage intdeclare @CurrentPageSize intdeclare @TotalRecordForPageIndex INT
BEGIN IF @Where is NULL SET @Where =n "--Total records DECLARE @countSql NVARCHAR (4000) if @RecordCount is NU LL BEGIN SET @countSql = ' SELECT @TotalRecord =count (*) from ' [Email protected]+ ' [email protected] EXECUTE    sp_executesql @countSql, N ' @TotalRecord int out ', @TotalRecord out END ELSE BEGIN SET @[email protected] END set @[email protected] Set @TotalPage = (@TotalRecord-1)/@PageSize +1 SET @CurrentPageSize = (@PageInde X-1) * @PageSize
    --returns the total number of pages and total records     set @[email protected]     SET @[email protected]            --return records      set @[email protected]* @PageSize         exec      (' SELECT *            from ( SELECT TOP ' [email protected]+ ' [email protected]+ ', Row_number () over (' [email protected]+ ') as Pageview_rowno            from ' [email  protected]+ ' + @Where + ') as temppageviewtable           & nbsp WHERE Temppageviewtable.pageview_rowno >               ' [email protected]     endreturn 0go            

SQL Server paging stored procedures

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.