SQL SERVER Universal Paging stored procedures

Source: Internet
Author: User

SQL SERVER Universal Paging stored procedures

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 @pagesize int,--Number of records per page @sort VARCHAR (255),--sort fields and rules without 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 recordsets asdeclare @strSql nvarchar (max) set NOCOUNT on;if (@IsGetCount = 1) begin set @strSql = ' SELECT COUNT (0) from ' + @tab + ' wher E ' + @strWhereendelsebegin  Set @strSql = ' SELECT * FROM (select Row_number () over   (ORDER by ' + @Sort + ') as Rownu M, ' + @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) set NOCOUNT off;

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,-- Start line number @pagesize int,--Number of records per page @sort VARCHAR (255),--sort fields and rules without 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 recordset) Asdeclare @strSql NVARCHAR (max) set NOCOUNT on;if (@IsGetCount = 1) BEGIN Set @strSql = ' SELECT COUNT (0) from  ' + @tab + ' WHERE ' + @strWhereENDELSEBEGIN  DECLARE @ENDINDEX INT  IF (@StartIndex >0)    BEGIN      SET @[email Protected][email protected]      set @[email protected]+1    END  ELSE    set @[email protected][email protected  ]         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) SET NOCOUNT OFF;

SQL SERVER Universal 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.