SQLServer 효율적인 페이지 나누기 저장 프로세스

2601 단어
*******************************************************************************************************************************************************************************************************************************************************/ALTER procedure [dbo].[PageQuery] @PrimaryKey varchar(100), - 주 키 @OrderBy varchar(100), - 정렬 필드 @SqlStr varchar(8000), - sql문장 @PageSize int,- 페이지당 표시된 레코드 @PageIndex int - 현재 페이지(1부터 시작) - @RecordCount int output - 반환된 총 레코드 수 as declare @ExecSql varchar(8000) - 실행할 Sql 조합 if @OrderBy ='set @OrderBy ='order by'+ @Primary Key +'asc'else if (len (@ OrderBy) <8 or upper(substring (@ Ordery 1, 8)(<'ORDER BY') set @OrderBy ='order by'+@OrderBy if @PageSize=-1 - aaaaaajax에 대한 첫 조회 set @PageSize=0 set =0 set @PageBy ize = 0 seaaaaaaaaaaaaaaax에 대한 set @PageSize=1 - 1 - - - - - - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaKey+') from('+@SqlStr+') TN'exec spexecuteSql @s,N'@recordCount int output',@recordCount output if(@pageSize>0) set @pageCount = (@recordCount - 1 + @PageSize)/@PageSize;--총 페이지 수 else set @pageCount = 0; -총 페이지 수
    if @PageIndex<=0--           
        begin
            set @ExecSql='select top '+cast(@PageSize as varchar(100))+' * from ('+@SqlStr+') T '+@OrderBy
        end
    else
        begin
            if charindex('2000 - 8.00.',@@version)>0
                begin
                    set @ExecSql=
                    'select top '+cast(@PageSize as varchar(100))+' *
                    from ('+@SqlStr+') as T where T.'+@PrimaryKey+' not in
                    (select top '+cast((@PageSize*@PageIndex) as varchar(100))+' '+@PrimaryKey+'
                        from ('+@SqlStr+') T2 '+@OrderBy+') '+ @OrderBy
                end
            else
                begin
                    set @ExecSql=
                    'select * from
                    (
                        select * from
                        (SELECT *, #RowNum#=ROW_NUMBER() OVER('+@OrderBy+') FROM ('+@SqlStr+') T1) T2 where T2.#RowNum#>='+cast(@PageSize*@PageIndex+1 as varchar(10))+' and T2.#RowNum#<='+cast(@PageSize*(@PageIndex+1) as varchar(10))+'
                    ) T3
                    '
                end
        end
        exec (@ExecSql+' select '+@recordCount+' as RecordCount,'+@pageCount+' as PageCount ')

end

좋은 웹페이지 즐겨찾기