SQL searchable sortable paging stored procedure for SQL 2008 or more

Source: Internet
Author: User
Tags sql 2008

-- =============================================--Author: Spider King--Create date:2015-10-29--Description: Searchable sortable pagination stored procedure for sql2008 or more--(very important, please use carefully)-- =============================================Create PROCEDUREDbo.list (@table varchar( +),--table name or view chart (required)        @orderby varchar( +),--sort field (required)        @fields varchar(Max)='*',--to select a field list (optional)        @pageindex int=1,--page number, starting from 1 (optional)        @pagesize int= -,--page size (optional)        @where nvarchar(Max)=N"',--conditions (optional)        @IsCount bit = 1 --returns the total number of records, with a value other than 0 (optional)    )  asBEGIN    SETNOCOUNT on    Declare @sqlstr nvarchar(Max)    Set @sqlstr="'    Set @orderby = 'Order by' + @orderby    if @where<>N"'        Set @where = 'Where' + @where        --on the first page you can use the top    if @pageindex=1    begin        Set @sqlstr = 'Select Top' + CONVERT(varchar(Ten),@pagesize)+ ' ' + @fields + ' from' + @table + @where + @orderby    End    Else    begin        Set @sqlstr = 'With Temptbl as ('        + 'SELECT row_number () over ('+ @orderby +') as Row,'        + @fields + ' from' + @table + @where        + ') SELECT'+ @fields + 'From Temptbl where Row between' + CONVERT(varchar(Ten),(@pageindex-1)*@pagesize+1)+ ' and' + CONVERT(varchar(Ten),(@pageindex-1)*@pagesize+@pagesize)            End    if @IsCount!=0        Set @sqlstr = @sqlstr + '; select count (1) from' + @table + @where    Execute(@sqlstr)END



--Test CodeexecList@table='DL_QQ',@orderby='IID desc',@IsCount=1,@pageindex =2,@fields='IID,QQ',@where='IID <30'execList@table='DL_QQ',@orderby='IID desc',@IsCount=1,@pageindex =1,@fields='IID,QQ',@where='IID <30'

SQL searchable sortable paging stored procedure for SQL 2008 or more

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.