A paging stored procedure that can query the total number of pages and total page count in a table __ stores

Source: Internet
Author: User

if exists (select 1 from sysobjects where name = ' proc_pagination ') drop proc proc_pagination go create proc Proc_paginatio N--Creates a paging stored procedure @page int = 1,--the number of pages @pagesize int = 10, the size of each page @tableName varchar (100), the table name to query @orderFields varchar (255 --The sorted field name (to be sure) @GetFields varchar (1000) = ' * ',--the column to be returned @sqlWhere varchar (1500) = ',--Query condition (Note: Do not add where, but add and) @Or Dertype varchar (4) = ' desc ',--set Sort type @totalrow int output,--How many data (output parameters) are queried @totalpage int output-Total number of pages (output parameters) as Decla Re @sql varchar (), @sumRowSql nvarchar (), @ParmDefinition nvarchar (200); Set @sumRowSql = N ' Select @totalRow = count (1) from ' + @tableName + ' WHERE 1 = 1 ' + @sqlWhere; --Query SQL statement Set @ParmDefinition = N ' @totalRow int output '; --Define the parameter type of the query SQL statement exec sp_executesql @sumRowSql, @ParmDefinition, @totalRow = @totalrow output--invokes the system stored procedure that executes the string. and receive the value of the parameter in the SQL statement set @sql = ' Select top ' + CONVERT (varchar (4), @pagesize) + "+ @GetFields + ' from ' + @tableName + ' wher E ' + @orderFields + ' not in (select Top ' + CoNvert (varchar (5), @pagesize * (@page-1)) + ' + @orderFields + ' from ' + @tableName + ' ORDER BY ' + @orderFields + ' + @ OrderType + ') and 1 = 1 ' + @sqlWhere + ' ORDER BY ' + @orderFields + ' + @OrderType; --print @sql; EXEC (@sql); Set @totalpage = (@pagesize + @totalrow-1)/@pagesize; --Count the number of pages go---------execute stored procedures----------declare @rows int, @page int exec proc_pagination @tableName = ' Tb_goodsbasicinfo ', @p Age = 2, @orderFields = ' Stylenumberid ', @sqlWhere = ' and 2 = 2 ', @totalrow = @rows output, @totalpage = @page Output SELECT @ Total number of rows, @page total pages

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.