SQL Server 比较实用的大数据量分页存储过程
发布时间:2020-05-28 03:29:23 所属栏目:MsSql 来源:互联网
导读:SQL Server 比较实用的大数据量分页存储过程
|
感兴趣的小伙伴,下面一起跟随脚本之家 jb51.cc的小编两巴掌来看看吧! create proc sp_PublicTurnPageWebSite(@TBName nvarchar(100)='', --表名,如 pinyin @PageSize int=10, --每页的记录数,默认为 10 @CurPage int=1, --表示当前页 1 @KeyField nvarchar(100)='ID', --关键字段名,默认为 ID,该字段要求是表中的索引 或 无重复和不为空的字段 @KeyAscDesc nvarchar(4)='ASC', --关键字的升、降序,默认为升序 ASC , 降序为 DESC @Fields nvarchar(500)='*', --所选择的列名,默认为全选 @Condition nvarchar(200)='', --where 条件,默认为空 @Order nvarchar(200)='' --排序条件,默认为空 ) with encryption as if @TBName = '' begin raiserror('请指定表名!',11,1) return end if @PageSize <=0 or @CurPage <0 begin raiserror('当前页数和每页的记录数都必须大于零!',1) return end if @KeyAscDesc = 'DESC' set @KeyAscDesc = '<' else set @KeyAscDesc = '>' if @Condition <> '' set @Condition = ' where ' + @Condition declare @SQL nvarchar(2000) set @SQL = '' if @CurPage = 1 set @SQL = @SQL + 'Select Top ' + cast(@PageSize as nvarchar(20)) + ' ' + @Fields + ' FROM ' + @TBName + @Condition + ' ' + @Order else begin declare @iTopNum int set @iTopNum = @PageSize * (@CurPage - 1) set @SQL = @SQL + 'declare @sLastValue nvarchar(100)' + char(13) set @SQL = @SQL + 'Select Top ' + cast(@iTopNum as nvarchar(20)) + ' @sLastValue=' + @KeyField + ' FROM ' + @TBName + @Condition + ' ' + @Order + char(13) declare @Condition2 nvarchar(200) if @Condition = '' set @Condition2 = ' where ' + @KeyField + @KeyAscDesc + '@sLastValue ' else set @Condition2 = ' and ' + @KeyField + @KeyAscDesc + '@sLastValue ' set @SQL = @SQL + 'Select Top ' + cast(@PageSize as nvarchar(20)) + ' ' + @Fields + ' FROM ' + @TBName + @Condition + @Condition2 + @Order end EXECUTE sp_executesql @SQL (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
