SQL SERVER Universal Paging stored procedures, either of which you choose

Source: Internet
Author: User

Write in front

Starting with SQL Server 2005, provided the Row_number () function, using the function generated by the index to handle paging, according to normal logical thinking is to pass pageindex and pagesize to complete the paging, the front end and I communicated yesterday, They use JQuery.DataTable.js plug-ins, and through the company's bottom-level package, pageindex need to change a bit, become pageindex*pagesize to preach. That is to show 30 per page count, the first pass 0, the second pass 30 so to calculate, I am also drunk.

1, transmission pageindex and pagesize
CREATE PROC [dbo].[common_pagelist](@tab nvarchar(Max),---table name@strFld nvarchar(Max),--Field String@strWhere varchar(Max),--Where Condition@PageIndex int,--Page Number@PageSize int,--number of records to hold per page@Sort VARCHAR(255),--sort fields and rules without adding an order by@IsGetCount bit  --Whether to get the total number of records, 1 to get the total number of records, 0 to not get the total number of records, return record set) asDeclare @strSql nvarchar(Max)SetNocount on;if(@IsGetCount = 1)begin Set @strSql='SELECT COUNT (0) from' + @tab + 'WHERE' + @strWhereEndElsebegin  Set @strSql='SELECT * FROM (select Row_number () over (ORDER by' + @Sort + ') as RowNum,' + @strFld + ' from' + @tab + 'where' + @strWhere + ') as Dwhere WHERE rownum between' + CAST(((@PageIndex-1)*@PageSize + 1) as nvarchar( -))+ ' and' + cast((@PageIndex*@PageSize) as nvarchar( -))Endexec(@strSql)SetNocountoff;

2, transmission pageindex*pagesize and pagesize
CREATE PROC [dbo].[Common_pagelist2](@tab nvarchar(Max),---table name@strFld nvarchar(Max),--Field String@strWhere varchar(Max),--Where Condition@StartIndex int,--number of start lines@PageSize int,--number of records to hold per page@Sort VARCHAR(255),--sort fields and rules without adding an order by@IsGetCount bit  --Whether to get the total number of records, 1 to get the total number of records, 0 to not get the total number of records, return record set) asDECLARE @strSql NVARCHAR(Max)SETNOCOUNT on;IF(@IsGetCount = 1)BEGIN SET @strSql='SELECT COUNT (0) from' + @tab + 'WHERE' + @strWhereENDELSEBEGIN  DECLARE @ENDINDEX INT  IF(@StartIndex>0)    BEGIN      SET @ENDINDEX=@StartIndex+@PageSize      SET @StartIndex=@StartIndex+1    END  ELSE    SET @ENDINDEX=@StartIndex+@PageSize         SET @strSql='SELECT * FROM (select Row_number () over (ORDER by' + @Sort + ') as RowNum,' + @strFld + ' from' + @tab + 'where' + @strWhere + ') as Dwhere WHERE rownum between' + CAST(@StartIndex  as VARCHAR( -))+ ' and' + CAST(@ENDINDEX  as VARCHAR( -))Endexec(@strSql)SETNOCOUNTOFF;

SQL SERVER Universal paging stored procedure, either of the two uses you choose

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.