sql: paging in SQL Server
--sql server 2012 及以上 SELECT * FROM BookKindList ORDER BY (SELECT NULL) OFFSET 0 ROWS FETCH NEXT 3 ROWS ONLY go -- geovindu Geovin Du 涂聚文 Declare @PageNo INT Declare @PageSize INT Set @PageNo=1 Set @PageSize=4 Select * From (Select ROW_NUMBER() Over (Order by BookKindID Desc) AS 'RowNum',* From BookKindList Where BookKindID > 0)t Where t.RowNum Between ((@PageNo-1)*@PageSize +1) AND (@PageNo*@pageSize) go Declare @PageNo INT Declare @PageSize INT Set @PageNo=2 Set @PageSize=4 Select * From (Select ROW_NUMBER() Over (Order by BookKindID Desc) AS 'RowNum',* From BookKindList Where BookKindID > 0)t Where t.RowNum Between ((@PageNo-1)*@PageSize +1) AND (@PageNo*@pageSize) go -- 2 - QUERY USING "ROW_NUMBER" DECLARE @PageNumber AS INT, @RowspPage AS INT SET @PageNumber = 1 SET @RowspPage = 4 SELECT * FROM ( SELECT ROW_NUMBER() OVER(ORDER BY BookKindID) AS Numero, * FROM BookKindList ) AS TBL WHERE BookKindID BETWEEN ((@PageNumber - 1) * @RowspPage + 1) AND (@PageNumber * @RowspPage) ORDER BY BookKindID GO -- 3 - QUERY USING "OFFSET" AND "FETCH NEXT" (SQL SERVER 2012) Geovin Du geovindu 涂聚文 DECLARE @PageNumber AS INT, @PageSize AS INT SET @PageNumber = 1 SET @PageSize = 4 SELECT * FROM BookKindList ORDER BY BookKindID OFFSET ((@PageNumber - 1) * @PageSize) ROWS FETCH NEXT @PageSize ROWS ONLY GO DECLARE @PageNumber AS INT, @PageSize AS INT,@totalcount INT,@pagecount int,@c int SET @PageNumber = 2 SET @PageSize = 4 SELECT * FROM BookKindList ORDER BY BookKindID OFFSET ((@PageNumber - 1) * @PageSize) ROWS FETCH NEXT @PageSize ROWS ONLY; SELECT @totalcount=count(*) FROM BookKindList --Select @c=@totalcount%@PageSize --Select @pagecount=@totalcount/@PageSize SELECT @totalcount as '总记录' --if @c>0 --Select @pagecount+1 as '共页数' --else --Select @pagecount as '共页数' --Select @c Select @pagecount=dbo.getPageModulus(@totalcount,@PageSize) GO --'*'#查询字段 涂聚文 Geovin Du geovindu --,'bookkindlist'#表名 --,'1=1'#条件 --,'BookKindID desc'#排序 --,1 #页码 --,5 #每页记录数 DECLARE @sql as nvarchar(2000), --- @sqlt as nvarchar(2000), @fields as nVARCHAR(1000), --要查询的字段,用逗号(,)分隔 @tables as nvarchar(150), --要查询的表 @where as nVARCHAR(2000), --查询条件 @orderby as nVARCHAR(200), ---BookKindID desc 排序规则 @pageindex as INT, --查询页码 geovindu @pageSize as INT, --每页记录数 @totalcount as INT, --总记录数 out @pagecount as INT --总页数 out set @fields='*'; set @tables='BookKindList'; set @where='1=1'; set @orderby='BookKindID desc' set @pageindex=1; set @pageSize=14; set @sql='SELECT '+@fields+' FROM '+@tables+'' if @where<>'' set @sql=@sql+' where '+@where if @orderby<>'' set @sql=@sql+' ORDER BY '+@orderby+'' set @sql=@sql+' OFFSET (('+CONVERT(nVARCHAR(20),@pageindex)+' - 1) * '+CONVERT(nVARCHAR(20),@PageSize)+') ROWS FETCH NEXT '+CONVERT(nVARCHAR(20),@PageSize)+' ROWS ONLY;' set @sqlt='SELECT @totalcount=count(*) FROM BookKindList' if @where<>'' set @sqlt= @sqlt+' where '+@where; --set @sql=@sql+' Select @pagecount=dbo.getPageModulus(@totalcount,@PageSize);' print(@sql); exec(@sql); EXEC sp_executesql @sqlt,N'@totalcount int OUTPUT',@totalcount OUTPUT Select @pagecount=dbo.getPageModulus(@totalcount,@PageSize) Select @pagecount as '总页数',@totalcount as '总记录' GO Declare @sql nvarchar(4000),@SqlWhere nvarchar(1000),@TableName nvarchar(100); Declare @totalRecord int; --Declare @TotalPage int; --计算总记录数 geovindu set @TableName='BookKindList' set @sqlWhere='1=1'; if (@SqlWhere='' or @sqlWhere=NULL) set @sql = 'select @totalRecord = count(*) from ' + @TableName else set @sql = 'select @totalRecord = count(*) from ' + @TableName + ' where ' + @sqlWhere EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数 select @totalRecord --计算总页数 print @Sql go declare @tableName nvarchar(100) declare @sqlQuery nvarchar(max) declare @fields varchar(500) set @tableName = 'BookKindList' set @fields = 'BookKindName,BookKindParent' set @sqlQuery = 'select ' + @fields + ' from ' + QUOTENAME(@tableName) execute sp_executesql @sqlQuery go
https://www.codeproject.com/articles/55616/custom-paging-stored-procedure
https://www.programmerall.com/article/37701010712/
According to TOP ID
CREATE PROC [dbo].[proc_select_page_top] @pageindex INT=1,--current page number @pagesize INT=10,--Page size @tablename VARCHAR(50)='',--Table Name @fields VARCHAR(1000)='',--Query field collection @keyid VARCHAR(50)='',--Primary key @condition NVARCHAR(1000)='',--Query conditions @orderstr VARCHAR(500),--Sort condition @totalRecord BIGINT OUTPUT--total AS IF ISNULL(@orderstr,N'')=N'' SET @orderstr=N' ORDER BY '+@keyid+N' DESC ' IF ISNULL(@fields,N'')=N'' SET @fields=N'*' IF ISNULL(@condition,N'')=N'' SET @condition=N'1=1' DECLARE @sql NVARCHAR(4000) --The total number of records in the table --IF(@totalRecord IS NULL) --BEGIN SET @sql=N'SELECT @totalRecord=COUNT(*)' +N' FROM '+@tablename +N' WHERE '+@condition EXEC sp_executesql @sql,N'@totalRecord INT OUTPUT',@totalRecord OUTPUT —END IF(@pageindex=1) BEGIN SET @sql=N'SELECT TOP '+STR(@pagesize)+N' '+@fields+N' FROM '+@tablename+N' WHERE '+@condition+N' '+@orderstr EXEC(@sql) END ELSE BEGIN SET @sql=N'SELECT TOP '+STR(@pagesize)+N' '+@fields+N' FROM '+@tablename+ N' WHERE '+@keyid+N' NOT IN(SELECT TOP '+STR((@pageindex-1)*@pagesize)+N' '+@keyid+ N' FROM '+@tablename+N' WHERE '+@condition+N' '+@orderstr+N') AND '+@condition+N' '+@orderstr EXEC(@sql) END GO
According to row_number () over
CREATE PROC [dbo].[proc_select_page_row] @pageindex INT=1,--current page number @pagesize INT=10,--Page size @tablename VARCHAR(50)='',--Table Name @fields VARCHAR(1000)='*',--Query field collection @keyid VARCHAR(50)='',--Primary key @condition NVARCHAR(1000)='',--Query conditions @orderstr VARCHAR(500),--Sort condition @totalRecord BIGINT OUTPUT--total AS IF ISNULL(@orderstr,N'')=N'' SET @orderstr=N' ORDER BY '+@keyid+N' DESC ' IF ISNULL(@fields,N'')=N'' SET @fields=N'*' IF ISNULL(@condition,N'')=N'' SET @condition=N'1=1' DECLARE @sql NVARCHAR(4000) --The total number of records in the table -- IF @totalRecord IS NULL -- BEGIN SET @sql=N'SELECT @totalRecord=COUNT(*)' +N' FROM '+@tablename +N' WHERE '+@condition EXEC sp_executesql @sql,N'@totalRecord bigint OUTPUT',@totalRecord OUTPUT --END IF(@pageindex=1) BEGIN SET @sql=N'SELECT TOP '+STR(@pagesize)+N' '+@fields+N' FROM '+@tablename+N' WHERE '+@condition+N' '+@orderstr EXEC(@sql) END ELSE BEGIN DECLARE @StartRecord INT SET @StartRecord = (@pageindex-1)*@pagesize + 1 SET @sql=N'SELECT * FROM (SELECT ROW_NUMBER() OVER ('+ @orderstr +N') AS rowId,'+@fields+ N' FROM '+ @tablename+N') AS T WHERE rowId>='+STR(@StartRecord)+ N' and rowId<='+STR(@StartRecord + @pagesize - 1) EXEC(@sql) END GO
According to the max (min) ID
--According to MAX(MIN)ID CREATE PROC [dbo].[proc_select_id] @pageindex int=1,--current page number @pagesize int=10,--Page size @tablename VARCHAR(50)='',--Table Name @fields VARCHAR(1000)='',--Query field collection @keyid VARCHAR(50)='',--Primary key @condition NVARCHAR(1000)='',--Query conditions @orderstr VARCHAR(500),--Sort condition @totalRecord BIGINT OUTPUT--total AS IF ISNULL(@orderstr,N'')=N'' SET @orderstr=N' ORDER BY '+@keyid+N' DESC ' IF ISNULL(@fields,N'')=N'' SET @fields=N'*' IF ISNULL(@condition,N'')=N'' SET @condition=N'1=1' DECLARE @sql NVARCHAR(4000) --The total number of records in the table --IF(@totalRecord IS NULL) --BEGIN SET @sql=N'SELECT @totalRecord=COUNT(*)' +N' FROM '+@tablename +N' WHERE '+@condition EXEC sp_executesql @sql,N'@totalRecord INT OUTPUT',@totalRecord OUTPUT --END IF(@pageindex=1) BEGIN SET @sql=N'SELECT TOP '+STR(@pagesize)+N' '+@fields+N' FROM '+@tablename+N' WHERE '+@condition+N' '+@orderstr EXEC(@sql) END ELSE BEGIN DECLARE @operatestr CHAR(3),@comparestr CHAR(1) SET @operatestr='MAX' SET @comparestr='>' IF(@orderstr<>'') BEGIN IF(CHARINDEX('desc',LOWER(@orderstr))<>0) BEGIN SET @operatestr='MIN' SET @comparestr='<' END END SET @sql=N'SELECT top '+STR(@pagesize)+N' '+@fields+N' FROM '+@tablename+N' WHERE '+@keyid+@comparestr +N'(SELECT '+@operatestr+N'('+@keyid+N') FROM '+@tablename+N' WHERE '+@keyid +N' IN (SELECT TOP '+STR((@pageindex-1)*@pagesize)+N' '+@keyid+N' FROM '+@tablename+N' WHERE ' +@condition+N' '+@orderstr+N')) AND '+@condition+N' '+@orderstr EXEC(@sql) END GO
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)