Increase paging query stored procedures using temporary tables and @ @RowCount in SQL Server 2005

Source: Internet
Author: User
Tags rowcount

The recent discovery of the performance of common query stored procedures for existing frameworks is slow, and the following code is carefully studied:

Alter PROCEDURE [dbo]. [Areaselect]


@PageSize int=0,


@CurrentPage Int=1,


@Identifier Int=null,


@ParentId Int=null,


@AreaLevel Int=null,


@Children Int=null,


@AreaName nvarchar (=null),


@Path nvarchar (MAX) =null,


@Status Int=null,


@Alt Int=null


as


BEGIN


SET NOCOUNT on;


IF (not @AreaName is NULL) SET @AreaName = '% ' + @AreaName + '% '


IF (not @Path is NULL) SET @Path = '% ' + @Path + '% '


IF (@PageSize >0)


BEGIN


DECLARE @TotalPage int


Select @TotalPage =count (Identifier) from the area Where


(@Identifier is NULL or identifier= @Identifier) and


(@ParentId is NULL or parentid= @ParentId) and


(@AreaLevel is NULL or arealevel= @AreaLevel) and


(@Children is NULL or children= @Children) and


(@AreaName is NULL or areaname like @AreaName) and


(@Path is NULL or Path like @Path) and


(@Status is NULL or status= @Status) and


(@Alt is NULL or alt= @Alt)


IF (@TotalPage% @PageSize =0)


BEGIN


SET @TotalPage = @TotalPage/@PageSize


End


ELSE


BEGIN


SET @TotalPage =round (@TotalPage/@PageSize, 0) +1


End


Select Top (@PageSize) Identifier,parentid,arealevel,children,areaname,path,status,alt, @TotalPage as Totalpage From area Where


Identifier not in (the Select Top (@PageSize * (@CurrentPage-1)) Identifier from area Where


(@Identifier is NULL or identifier= @Identifier) and


(@ParentId is NULL or parentid= @ParentId) and


(@AreaLevel is NULL or arealevel= @AreaLevel) and


(@Children is NULL or children= @Children) and


(@AreaName is NULL or areaname like @AreaName) and


(@Path is NULL or Path like @Path) and


(@Status is NULL or status= @Status) and


(@Alt is NULL or alt= @Alt)


ORDER BY areaname ASC)


and


(@Identifier is NULL or identifier= @Identifier) and


(@ParentId is NULL or parentid= @ParentId) and


(@AreaLevel is NULL or arealevel= @AreaLevel) and


(@Children is NULL or children= @Children) and


(@AreaName is NULL or areaname like @AreaName) and


(@Path is NULL or Path like @Path) and


(@Status is NULL or status= @Status) and


(@Alt is NULL or alt= @Alt)


ORDER BY areaname ASC


End


ELSE


BEGIN


Select Identifier,parentid,arealevel,children,areaname,path,status,alt from area Where


(@Identifier is NULL or identifier= @Identifier) and


(@ParentId is NULL or parentid= @ParentId) and


(@AreaLevel is NULL or arealevel= @AreaLevel) and


(@Children is NULL or children= @Children) and


(@AreaName is NULL or areaname like @AreaName) and


(@Path is NULL or Path like @Path) and


(@Status is NULL or status= @Status) and


(@Alt is NULL or alt= @Alt)


ORDER BY areaname ASC


End


End

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.