저장 프로세스 페이지 나누기 문장

11035 단어 저장 프로세스
create proc [dbo].[ceb_Pagination]

@strFields nvarchar(2000),  --   

@strTableName nvarchar(2000), --  

@strWhere nvarchar(4000),  --      where

@strOrderBy nvarchar(200), --         order by

@PageSize int,    --    

@CurrentPage int,   --   ,1    

@PageCount int output,  --     

@RecordCount int output  --      

as

begin

declare @StartIndex int     --      

set @StartIndex = (@currentPage - 1) * @PageSize + 1

declare @strSql1 nvarchar (Max) --    

declare @strSql2 nvarchar (Max) --      

declare @ParmDefinition nvarchar (Max)

set @ParmDefinition = N'@tmp int output'

set @strSql1 = N'select row_number() over (order by ' + @strOrderBy + ' ) as RowID, '

set @strSql2 = 'select @tmp = count(*) '

if @strFields <> ''

 set @strSql1 = @strSql1 + @strFields

else

 set @strSql1 = @strSql1 + ' * '

if @strTableName <> ''

begin

 set @strSql1 = @strSql1 + ' from ' + @strTableName

 set @strSql2 = @strSql2 + ' from ' + @strTableName

end

if @strWhere <> ''

begin

 set @strSql1 = @strSql1 + ' where ' + @strWhere

 set @strSql2 = @strSql2 + ' where ' + @strWhere

end

exec sp_executesql @strSql2,@ParmDefinition,@tmp = @RecordCount output  --        SQL  



if @RecordCount % @PageSize = 0  --     

 set @PageCount = @RecordCount / @PageSize

else

 set @PageCount = @RecordCount / @PageSize + 1

set @strSql1 = 'with TempTable as ( ' + @strSql1 + ' ) select * from TempTable where RowID between ' 

  + Convert(varchar(10),@StartIndex) + ' and ' + Convert(varchar(10),@StartIndex + @PageSize - 1)

SET @strSql1 =@strSql1 +' order by RowID'

exec(@strSql1)

좋은 웹페이지 즐겨찾기