时间:2021-07-01 10:21:17 帮助过:20人阅读
CREATE PROC [dbo].[Common_PageList] ( @tab nvarchar(max),---表名 @strFld nvarchar(max), --字段字符串 @strWhere varchar(max), --where条件 @PageIndex int, --页码 @PageSize int, --每页容纳的记录数 @Sort VARCHAR(255), --排序字段及规则,不用加order by @IsGetCount bit --是否得到记录总数,1为得到记录总数,0为不得到记录总数,返回记录集 ) AS declare @strSql nvarchar(max) set nocount on; if(@IsGetCount = 1) begin set @strSql=‘SELECT COUNT(0) FROM ‘ + @tab + ‘ WHERE ‘ + @strWhere end else begin set @strSql=‘ SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY ‘ + @Sort + ‘) AS rownum, ‘ + @strFld + ‘ FROM ‘ + @tab + ‘ where ‘ + @strWhere + ‘) AS Dwhere WHERE rownum BETWEEN ‘ + CAST(((@PageIndex-1)*@PageSize + 1) as nvarchar(20)) + ‘ and ‘ + cast((@PageIndex*@PageSize) as nvarchar(20)) end exec (@strSql) set nocount off;
CREATE PROC [dbo].[Common_PageList2] ( @tab nvarchar(max),---表名 @strFld nvarchar(max), --字段字符串 @strWhere varchar(max), --where条件 @StartIndex int, -- 开始行数 @PageSize int, --每页容纳的记录数 @Sort VARCHAR(255), --排序字段及规则,不用加order by @IsGetCount bit --是否得到记录总数,1为得到记录总数,0为不得到记录总数,返回记录集 ) AS DECLARE @strSql NVARCHAR(max) SET NOCOUNT ON; IF(@IsGetCount = 1) BEGIN SET @strSql=‘SELECT COUNT(0) FROM ‘ + @tab + ‘ WHERE ‘ + @strWhere END ELSE BEGIN DECLARE @ENDINDEX INT IF(@StartIndex>0) BEGIN SET @ENDINDEX=@StartIndex+@PageSize SET @StartIndex=@StartIndex+1 END ELSE SET @ENDINDEX=@StartIndex+@PageSize SET @strSql=‘ SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY ‘ + @Sort + ‘) AS rownum, ‘ + @strFld + ‘ FROM ‘ + @tab + ‘ where ‘ + @strWhere + ‘) AS Dwhere WHERE rownum BETWEEN ‘ + CAST(@StartIndex AS VARCHAR(30)) + ‘ and ‘ + CAST(@ENDINDEX AS VARCHAR(30)) end exec (@strSql) SET NOCOUNT OFF;
SQL SERVER 通用分页存储过程
标签:sql 二次 nbsp varchar bit 处理 前端 jquery 完成