------ row number ---------------- row number ----------declare @pageSize int,@pageIndex intset @pageSize = 20;set @pageIndex = 2select * from (SELECT...
------ row number ----------
------ row number ---------- declare @pageSize int,@pageIndex int set @pageSize = 20;set @pageIndex = 2 select * from ( SELECT ROW_NUMBER() OVER(ORDER BY Byod_ZZFP.ID DESC) as rowID ,Byod_ZZFP.* from Byod_ZZFP where 1=1 )T where (rowID > @pageSize * (@pageIndex - 1) and rowID <= @pageSize * (@pageIndex)) order by ID desc -- CTE表達式 -- declare @pageSize int,@pageIndex int set @pageSize = 20;set @pageIndex = 2; with T as ( SELECT ROW_NUMBER() OVER(ORDER BY Byod_ZZFP.ID DESC) as rowID ,Byod_ZZFP.* from Byod_ZZFP where 1=1 ) select * from T where (rowID > @pageSize * (@pageIndex - 1) and rowID <= @pageSize * (@pageIndex)) order by ID desc
------ max/min ----------
CREATE PROC [dbo].[uspLGetSolutionList] @pageSize INT, --頁碼大小 @pageIndex INT, --頁碼 @strWhere nvarchar(2000)='', @totalRecordCount INT OUTPUT --總記錄數 AS DECLARE @strSql NVARCHAR(MAX) --sql語句 DECLARE @sqlcount INT --返回總記錄 DECLARE @strSqlCount NVARCHAR(MAX) --sql語句1,總記錄數語句 DECLARE @tempSql NVARCHAR(MAX) --查詢欄位 DECLARE @temTableOn NVARCHAR(2000) --表連接及表連接關係 DECLARE @order NVARCHAR(500) --排序 SET @tempSql = ' NetworkCutover.ID ' SET @temTableOn = ' NetworkCutover NetworkCutover inner join Member Member on NetworkCutover.DutyMember = Member.MemberID ' SET @strSql = ' '; SET @order = ' ORDER BY NetworkCutover.ID desc ' IF(@pageIndex <= 1) BEGIN SET @strSql = 'SELECT TOP '+STR(@pageSize) + @tempSql +' FROM ' + @temTableOn + ' WHERE 1=1 ' + @strWhere + @order; END ELSE BEGIN SET @strSql = 'SELECT TOP '+STR(@pageSize)+@tempSql +' FROM '+@temTableOn +' WHERE NetworkCutover.ID < (SELECT MIN(T.ID) FROM (SELECT TOP '+STR(@pageSize*(@pageIndex-1))+' NetworkCutover.ID FROM '+@temTableOn+' WHERE 1=1 '+@strWhere +@order +') T)'+@strWhere+ @order END print @strSql SET @strSqlCount='SELECT @sqlcount=COUNT(*) FROM '+@temTableOn+' WHERE 1=1 '+@strWhere print @strSqlCount EXEC SP_EXECUTESQL @strSqlCount,N'@sqlcount INT OUTPUT',@sqlcount OUTPUT SET @totalRecordCount=@sqlcount EXEC(@strSql) GOView Code