Stored Procedures Manual Paging SQL

Source: Internet
Author: User

CREATE procedure News_class

--Information News millions pagination

(

@StrWhere varchar,--conditions

@PageSize int,--page size

@PageIndex INT--page index

)

As

DECLARE @strSQL varchar (2000)--the subject sentence

DECLARE @strCountSQL varchar (2000)--general record subject sentence

DECLARE @strTmp varchar (1000)--Temporary variable

Set @strTmp = ' Select top ' + str (@PageSize) + ' title,addtime from Tb_news '--note here that several fields are required to read a few fields

If @StrWhere <> '

Begin

Set @[email protected] + ' where ID < (select min (ID) from (select top ' + str ((@PageIndex-1) * @PageSize) + ' ID from Tb_n EWS Where ' [email protected]+ ' ORDER BY id DESC] as tbltmp) and ' [email protected]+ ' ORDER BY id DESC '

Set @strCountSQL = ' Select count (ID) as Countx from Tb_news Where ' [email protected]+ '

End

Else

Begin

Set @[email protected] + ' where ID < (select min (ID) from (select top ' + str ((@PageIndex-1) * @PageSize) + ' ID from Tb_n EWS ORDER BY id DESC) as TBLTMP) Order by id DESC '

Set @strCountSQL = ' Select count (ID) as Countx from Tb_news '

End

If @PageIndex = 1

If @StrWhere <> '

Begin

Set @[email protected] + ' Where ' [email protected]+ ' ORDER BY ID Desc '

End

Else

Begin

Set @[email protected] + ' ORDER BY ID Desc '

End

EXEC (@strSQL)

EXEC (@strCountSQL)

GO

Stored Procedures Manual Paging SQL

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.