SQL Server SQL paging (for personal use)

Source: Internet
Author: User

Here is the creation of the stored procedure, which is called when it is used.

/** * * * object:storedprocedure [dbo]. [Sq_paging] Script date:09/10/2014 09:57:54 * * * * **/SETAnsi_nulls onGOSETQuoted_identifier onGOCREATE proc [dbo].[Yc_pageselect]@tbName varchar(255),--Table name@tbFields varchar( +),--return field@PageSize int,--Page Size@PageIndex int,--Page Number@strWhere varchar( +),--Query Criteria@StrOrder varchar(255),--Sorting Criteria@Key varchar( -)--Sort Fields asDeclare @strSql varchar( the)--subject SentenceDeclare @strSqlCount nvarchar( -)--Query the total number of records subject sentence--total number of records---------------------------if @strWhere !="'beginSet @strSqlCount='Select COUNT (*) as Totalcout from' + @tbName + 'where'+ @strWhereEndElsebeginSet @strSqlCount='Select COUNT (*) as Totalcout from' + @tbNameEnd--------------Paging------------if @PageIndex <= 1beginSet@strSql='Select Top'+Str(@PageSize)+' '+@tbFields+' from' + @tbName+ 'where' + @strWhere +' ' + @strOrderEndElsebeginSet @strSql='Select Top'+Str(@PageSize)+' '+@tbFields+' from' + @tbName + 'where' + @strWhere+' and'+@Key +'> (select MAX ('+@Key+') from (select Top'+Str((@PageIndex-1)*@PageSize)+' '+@Key +' from' + @tbName + 'where' + @strWhere +' '+@strOrder +') a)'+ @strOrderEndexec(@strSqlCount)exec(@strSql)GO

SQL Server SQL paging (for personal use)

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.