本文转自:

版权声明:作者:jiankunking 出处:http://blog.csdn.net/jiankunking  本文版权归作者和CSDN共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

分页存储过程一:

[sql] view plaincopy print?
  1. --/*-----存储过程 分页处理 孙伟 2005-03-28创建 -------*/
  2. --/*----- 对数据进行了2分处理使查询前半部分数据与查询后半部分数据性能相同 -------*/
  3. --/*-----存储过程 分页处理 孙伟 2005-04-21修改 添加Distinct查询功能-------*/
  4. --/*-----存储过程 分页处理 孙伟 2005-05-18修改 多字段排序规则问题-------*/
  5. --/*-----存储过程 分页处理 孙伟 2005-06-15修改 多字段排序修改-------*/
  6. --/*-----存储过程 分页处理 孙伟 2005-12-13修改 修改数据分页方式为top max模式性能有极大提高-------*/
  7. --/*-----缺点:相对之前的not in版本主键只能是整型字段,如主键为GUID类型请使用not in 模式的版本-------*/
  8. CREATE PROCEDURE dbo.proc_ListPageInt
  9. (
  10. @tblName nvarchar(200), ----要显示的表或多个表的连接
  11. @fldName nvarchar(500) = '*', ----要显示的字段列表
  12. @pageSize int = 10, ----每页显示的记录个数
  13. @page int = 1, ----要显示那一页的记录
  14. @pageCount int = 1 output, ----查询结果分页后的总页数
  15. @Counts int = 1 output, ----查询到的记录数
  16. @fldSort nvarchar(200) = null, ----排序字段列表或条件
  17. @Sort bit = 0, ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ')
  18. @strCondition nvarchar(1000) = null, ----查询条件,不需where
  19. @ID nvarchar(150), ----主表的主键
  20. @Dist bit = 0 ----是否添加查询字段的 DISTINCT 默认0不添加/1添加
  21. )
  22. AS
  23. SET NOCOUNT ON
  24. Declare @sqlTmp nvarchar(1000) ----存放动态生成的SQL语句
  25. Declare @strTmp nvarchar(1000) ----存放取得查询结果总数的查询语句
  26. Declare @strID nvarchar(1000) ----存放取得查询开头或结尾ID的查询语句
  27. Declare @strSortType nvarchar(10) ----数据排序规则A
  28. Declare @strFSortType nvarchar(10) ----数据排序规则B
  29. Declare @SqlSelect nvarchar(50) ----对含有DISTINCT的查询进行SQL构造
  30. Declare @SqlCounts nvarchar(50) ----对含有DISTINCT的总数查询进行SQL构造
  31. if @Dist = 0
  32. begin
  33. set @SqlSelect = 'select '
  34. set @SqlCounts = 'Count(*)'
  35. end
  36. else
  37. begin
  38. set @SqlSelect = 'select distinct '
  39. set @SqlCounts = 'Count(DISTINCT '+@ID+')'
  40. end
  41. if @Sort=0
  42. begin
  43. set @strFSortType=' ASC '
  44. set @strSortType=' DESC '
  45. end
  46. else
  47. begin
  48. set @strFSortType=' DESC '
  49. set @strSortType=' ASC '
  50. end
  51. --------生成查询语句--------
  52. --此处@strTmp为取得查询结果数量的语句
  53. if @strCondition is null or @strCondition='' --没有设置显示条件
  54. begin
  55. set @sqlTmp = @fldName + ' From ' + @tblName
  56. set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName
  57. set @strID = ' From ' + @tblName
  58. end
  59. else
  60. begin
  61. set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition
  62. set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition
  63. set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition
  64. end
  65. ----取得查询结果总数量-----
  66. exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
  67. declare @tmpCounts int
  68. if @Counts = 0
  69. set @tmpCounts = 1
  70. else
  71. set @tmpCounts = @Counts
  72. --取得分页总数
  73. set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize
  74. /**//**当前页大于总页数 取最后一页**/
  75. if @page>@pageCount
  76. set @page=@pageCount
  77. --/*-----数据分页2分处理-------*/
  78. declare @pageIndex int --总数/页大小
  79. declare @lastcount int --总数%页大小
  80. set @pageIndex = @tmpCounts/@pageSize
  81. set @lastcount = @tmpCounts%@pageSize
  82. if @lastcount > 0
  83. set @pageIndex = @pageIndex + 1
  84. else
  85. set @lastcount = @pagesize
  86. --//***显示分页
  87. if @strCondition is null or @strCondition='' --没有设置显示条件
  88. begin
  89. if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分数据处理
  90. begin
  91. if @page=1
  92. set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
  93. +' order by '+ @fldSort +' '+ @strFSortType
  94. else
  95. begin
  96. set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
  97. +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
  98. +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'
  99. +' order by '+ @fldSort +' '+ @strFSortType
  100. end
  101. end
  102. else
  103. begin
  104. set @page = @pageIndex-@page+1 --后半部分数据处理
  105. if @page <= 1 --最后一页数据显示
  106. set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
  107. +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
  108. else
  109. set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
  110. +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
  111. +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'
  112. +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
  113. end
  114. end
  115. else --有查询条件
  116. begin
  117. if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分数据处理
  118. begin
  119. if @page=1
  120. set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
  121. +' where 1=1 ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType
  122. else
  123. begin
  124. set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
  125. +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
  126. +' where (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'
  127. +' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType
  128. end
  129. end
  130. else
  131. begin
  132. set @page = @pageIndex-@page+1 --后半部分数据处理
  133. if @page <= 1 --最后一页数据显示
  134. set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
  135. +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
  136. else
  137. set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
  138. +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
  139. +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'
  140. +' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
  141. end
  142. end
  143. ------返回查询结果-----
  144. exec sp_executesql @strTmp
  145. --print @strTmp
  146. SET NOCOUNT OFF
  147. GO
--/*-----存储过程 分页处理 孙伟 2005-03-28创建 -------*/
--/*----- 对数据进行了2分处理使查询前半部分数据与查询后半部分数据性能相同 -------*/
--/*-----存储过程 分页处理 孙伟 2005-04-21修改 添加Distinct查询功能-------*/
--/*-----存储过程 分页处理 孙伟 2005-05-18修改 多字段排序规则问题-------*/
--/*-----存储过程 分页处理 孙伟 2005-06-15修改 多字段排序修改-------*/
--/*-----存储过程 分页处理 孙伟 2005-12-13修改 修改数据分页方式为top max模式性能有极大提高-------*/
--/*-----缺点:相对之前的not in版本主键只能是整型字段,如主键为GUID类型请使用not in 模式的版本-------*/
CREATE PROCEDURE dbo.proc_ListPageInt
(
@tblName nvarchar(200), ----要显示的表或多个表的连接
@fldName nvarchar(500) = '*', ----要显示的字段列表
@pageSize int = 10, ----每页显示的记录个数
@page int = 1, ----要显示那一页的记录
@pageCount int = 1 output, ----查询结果分页后的总页数
@Counts int = 1 output, ----查询到的记录数
@fldSort nvarchar(200) = null, ----排序字段列表或条件
@Sort bit = 0, ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ')
@strCondition nvarchar(1000) = null, ----查询条件,不需where
@ID nvarchar(150), ----主表的主键
@Dist bit = 0 ----是否添加查询字段的 DISTINCT 默认0不添加/1添加
)
AS
SET NOCOUNT ON
Declare @sqlTmp nvarchar(1000) ----存放动态生成的SQL语句
Declare @strTmp nvarchar(1000) ----存放取得查询结果总数的查询语句
Declare @strID nvarchar(1000) ----存放取得查询开头或结尾ID的查询语句 Declare @strSortType nvarchar(10) ----数据排序规则A
Declare @strFSortType nvarchar(10) ----数据排序规则B Declare @SqlSelect nvarchar(50) ----对含有DISTINCT的查询进行SQL构造
Declare @SqlCounts nvarchar(50) ----对含有DISTINCT的总数查询进行SQL构造 if @Dist = 0
begin
set @SqlSelect = 'select '
set @SqlCounts = 'Count(*)'
end
else
begin
set @SqlSelect = 'select distinct '
set @SqlCounts = 'Count(DISTINCT '+@ID+')'
end if @Sort=0
begin
set @strFSortType=' ASC '
set @strSortType=' DESC '
end
else
begin
set @strFSortType=' DESC '
set @strSortType=' ASC '
end --------生成查询语句--------
--此处@strTmp为取得查询结果数量的语句
if @strCondition is null or @strCondition='' --没有设置显示条件
begin
set @sqlTmp = @fldName + ' From ' + @tblName
set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName
set @strID = ' From ' + @tblName
end
else
begin
set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition
set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition
set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition
end ----取得查询结果总数量-----
exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
declare @tmpCounts int
if @Counts = 0
set @tmpCounts = 1
else
set @tmpCounts = @Counts --取得分页总数
set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize /**//**当前页大于总页数 取最后一页**/
if @page>@pageCount
set @page=@pageCount --/*-----数据分页2分处理-------*/
declare @pageIndex int --总数/页大小
declare @lastcount int --总数%页大小 set @pageIndex = @tmpCounts/@pageSize
set @lastcount = @tmpCounts%@pageSize
if @lastcount > 0
set @pageIndex = @pageIndex + 1
else
set @lastcount = @pagesize --//***显示分页
if @strCondition is null or @strCondition='' --没有设置显示条件
begin
if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分数据处理
begin
if @page=1
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' order by '+ @fldSort +' '+ @strFSortType
else
begin
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
+' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'
+' order by '+ @fldSort +' '+ @strFSortType
end
end
else
begin
set @page = @pageIndex-@page+1 --后半部分数据处理
if @page <= 1 --最后一页数据显示
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
else
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
+' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'
+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
end
end else --有查询条件
begin
if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分数据处理
begin
if @page=1
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where 1=1 ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType
else
begin
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
+' where (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'
+' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType
end
end
else
begin
set @page = @pageIndex-@page+1 --后半部分数据处理
if @page <= 1 --最后一页数据显示
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
else
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
+' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'
+' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
end
end ------返回查询结果-----
exec sp_executesql @strTmp
--print @strTmp
SET NOCOUNT OFF
GO 

怎么在数据库中测试呢?

[sql] view plaincopy print?
  1. declare @pageCount int
  2. declare @Counts int
  3. exec [dbo].[proc_ListPageInt] 'sysobjects', '* ', 20,1,@pageCount output,@Counts output,'id', 0,'','id',0
  4. print @pageCount --这个可有可无
  5. print @Counts --这个可有可无
declare @pageCount int
declare @Counts int
exec [dbo].[proc_ListPageInt] 'sysobjects', '* ', 20,1,@pageCount output,@Counts output,'id', 0,'','id',0
print @pageCount --这个可有可无
print @Counts --这个可有可无

执行效果如下:

分页存储过程二:

[sql] view plaincopy print?
  1. USE [JianKunKingTestDatabase001]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[A_P_HelpPageShow] Script Date: 01/21/2015 19:19:42 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. ALTER PROCEDURE [dbo].[A_P_HelpPageShow]
  9. (
  10. @tblName nvarchar(max), -- 表名
  11. @strGetFields varchar(1000) = '*', -- 需要返回的列
  12. @fldName varchar(255)='', -- 排序的字段名
  13. @PageSize int = 10, -- 页尺寸
  14. @PageIndex int = 1, -- 页码
  15. @OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
  16. @strWhere varchar(1500) = '', -- 查询条件 (注意: 不要加 where)
  17. @Counts int = 0 output --查询到的记录数
  18. )
  19. AS
  20. declare @strSQL nvarchar(4000) -- 主语句
  21. declare @strTmp nvarchar(110) -- 临时变量
  22. declare @strOrder nvarchar(400) -- 排序类型
  23. declare @totalRecord int --查询到的记录数
  24. declare @SqlCounts nvarchar(max) ----对总数查询进行SQL构造
  25. --计算总记录数
  26. begin
  27. if @strWhere !=''
  28. set @SqlCounts = 'select @totalRecord=count(*) from ' + @tblName + ' where '+@strWhere
  29. else
  30. set @SqlCounts = 'select @totalRecord=count(*) from ' + @tblName + ''
  31. end
  32. --print @strWhere
  33. --print @SqlCounts
  34. exec sp_executesql @SqlCounts,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数
  35. set @Counts=@totalRecord
  36. begin
  37. if @OrderType != 0
  38. begin
  39. set @strTmp = '<(select min'
  40. set @strOrder = ' order by ' + @fldName +' desc'
  41. --如果@OrderType不是0,就执行降序,这句很重要!
  42. end
  43. else
  44. begin
  45. set @strTmp = '>(select max'
  46. set @strOrder = ' order by ' + @fldName +' asc'
  47. end
  48. --print @strOrder
  49. if @PageIndex = 1
  50. begin
  51. if @strWhere != ''
  52. set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ' + @tblName + ' where ' + @strWhere + ' ' + @strOrder
  53. else
  54. set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '+ @tblName + ' '+ @strOrder
  55. --如果是第一页就执行以上代码,这样会加快执行速度
  56. end
  57. else
  58. begin
  59. --以下代码赋予了@strSQL以真正执行的SQL代码
  60. set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '
  61. + @tblName + ' where ' + @fldName + ' ' + @strTmp + '('+ @fldName + ') from (select top ' + str((@PageIndex-1)*@PageSize) + ' '+ @fldName + ' from ' + @tblName + ' ' + @strOrder + ') as tblTmp)'+ @strOrder
  62. --print @strSQL
  63. if @strWhere != ''
  64. set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '
  65. + @tblName + ' where ' + @fldName + ' ' + @strTmp + '('
  66. + @fldName + ') from (select top ' + str((@PageIndex-1)*@PageSize) + ' '
  67. + @fldName + ' from ' + @tblName + ' where ' + @strWhere + ' '
  68. + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
  69. end
  70. end
  71. --print @strSQL
  72. exec sp_executesql @strSQL
  73. GO
USE [JianKunKingTestDatabase001]
GO /****** Object: StoredProcedure [dbo].[A_P_HelpPageShow] Script Date: 01/21/2015 19:19:42 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO ALTER PROCEDURE [dbo].[A_P_HelpPageShow]
(
@tblName nvarchar(max), -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(255)='', -- 排序的字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1500) = '', -- 查询条件 (注意: 不要加 where)
@Counts int = 0 output --查询到的记录数
)
AS
declare @strSQL nvarchar(4000) -- 主语句
declare @strTmp nvarchar(110) -- 临时变量
declare @strOrder nvarchar(400) -- 排序类型
declare @totalRecord int --查询到的记录数
declare @SqlCounts nvarchar(max) ----对总数查询进行SQL构造
--计算总记录数
begin
if @strWhere !=''
set @SqlCounts = 'select @totalRecord=count(*) from ' + @tblName + ' where '+@strWhere
else
set @SqlCounts = 'select @totalRecord=count(*) from ' + @tblName + ''
end --print @strWhere
--print @SqlCounts
exec sp_executesql @SqlCounts,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数
set @Counts=@totalRecord begin if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by ' + @fldName +' desc'
--如果@OrderType不是0,就执行降序,这句很重要!
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by ' + @fldName +' asc'
end
--print @strOrder
if @PageIndex = 1
begin
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ' + @tblName + ' where ' + @strWhere + ' ' + @strOrder
else
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '+ @tblName + ' '+ @strOrder
--如果是第一页就执行以上代码,这样会加快执行速度
end
else
begin
--以下代码赋予了@strSQL以真正执行的SQL代码
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '
+ @tblName + ' where ' + @fldName + ' ' + @strTmp + '('+ @fldName + ') from (select top ' + str((@PageIndex-1)*@PageSize) + ' '+ @fldName + ' from ' + @tblName + ' ' + @strOrder + ') as tblTmp)'+ @strOrder
--print @strSQL
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '
+ @tblName + ' where ' + @fldName + ' ' + @strTmp + '('
+ @fldName + ') from (select top ' + str((@PageIndex-1)*@PageSize) + ' '
+ @fldName + ' from ' + @tblName + ' where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
end
end
--print @strSQL
exec sp_executesql @strSQL
GO 

怎么在数据库中测试呢?

[sql] view plaincopy print?
  1. select count(*) from sysobjects ;
  2. declare @CountsAA int
  3. exec [dbo].[A_P_HelpPageShow] 'sysobjects', '* ', 'id ',20, 1,1, ' ',@CountsAA output
  4. print @CountsAA --这个可有可无
select count(*) from sysobjects ;
declare @CountsAA int
exec [dbo].[A_P_HelpPageShow] 'sysobjects', '* ', 'id ',20, 1,1, ' ',@CountsAA output
print @CountsAA --这个可有可无

执行结果如下:

存储过程二(优化版)

[sql] view plaincopy print?
  1. USE [JianKunKingTestDatabase001]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[A_P_HelpPageShow]    Script Date: 01/30/2015 20:21:13 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. CREATE  PROCEDURE [dbo].[A_P_HelpPageShow]
  9. (
  10. @TableName   nvarchar(max),       -- 表名
  11. @strGetFields varchar(1000) = '*', -- 需要返回的列
  12. @OrderField varchar(255)='',      -- 排序的字段名
  13. @PageSize   int = 10,          -- 页尺寸
  14. @PageIndex int = 1,          -- 页码
  15. @strWhere varchar(1500) = '', -- 查询条件 (注意: 不要加 where)
  16. @Counts    int = 0  output  --查询到的记录数
  17. )
  18. AS
  19. declare @strSQL   nvarchar(4000)       -- 主语句
  20. declare @totalRecord int  --查询到的记录数
  21. declare @SqlCounts nvarchar(max)     ----对总数查询进行SQL构造
  22. --计算总记录数
  23. begin
  24. if @strWhere !=''
  25. set @SqlCounts = 'select @totalRecord=count(*)  from ' + @TableName + ' where '+@strWhere
  26. else
  27. set @SqlCounts = 'select @totalRecord=count(*)  from ' + @TableName + ''
  28. end
  29. exec sp_executesql @SqlCounts,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数
  30. set  @Counts=@totalRecord
  31. BEGIN
  32. IF (@strWhere='' or @strWhere IS NULL)
  33. SET @strSQL = 'Select * FROM (select ' + @strGetFields + ',ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId from ' + @TableName
  34. ELSE
  35. SET @strSQL = 'Select * FROM (select ' + @strGetFields + ',ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId from ' + @TableName + ' where ' + @strWhere
  36. END
  37. --处理页数超出范围情况
  38. IF @PageIndex<=0
  39. SET @PageIndex = 1
  40. --处理开始点和结束点
  41. DECLARE @StartRecord INT
  42. DECLARE @EndRecord int
  43. SET @StartRecord = (@pageIndex-1)*@PageSize + 1
  44. SET @EndRecord = @StartRecord + @PageSize - 1
  45. --继续合成sql语句
  46. SET @strSQL = @strSQL + ') as tempTable where rowId >=' + CONVERT(VARCHAR(50),@StartRecord) + ' and rowid<= ' + CONVERT(VARCHAR(50),@EndRecord)
  47. exec sp_executesql @strSQL
  48. GO
USE [JianKunKingTestDatabase001]
GO/****** Object:  StoredProcedure [dbo].[A_P_HelpPageShow]    Script Date: 01/30/2015 20:21:13 ******/
SET ANSI_NULLS ON
GOSET QUOTED_IDENTIFIER ON
GOCREATE  PROCEDURE [dbo].[A_P_HelpPageShow]
(
@TableName   nvarchar(max),       -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@OrderField varchar(255)='',      -- 排序的字段名
@PageSize   int = 10,          -- 页尺寸
@PageIndex int = 1,          -- 页码
@strWhere varchar(1500) = '', -- 查询条件 (注意: 不要加 where)
@Counts    int = 0  output  --查询到的记录数
)
AS
declare @strSQL   nvarchar(4000)       -- 主语句
declare @totalRecord int  --查询到的记录数
declare @SqlCounts nvarchar(max)     ----对总数查询进行SQL构造
--计算总记录数
beginif @strWhere !=''
set @SqlCounts = 'select @totalRecord=count(*)  from ' + @TableName + ' where '+@strWhereelse
set @SqlCounts = 'select @totalRecord=count(*)  from ' + @TableName + ''
end exec sp_executesql @SqlCounts,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数
set  @Counts=@totalRecord BEGIN
IF (@strWhere='' or @strWhere IS NULL)
SET @strSQL = 'Select * FROM (select ' + @strGetFields + ',ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId from ' + @TableName
ELSE
SET @strSQL = 'Select * FROM (select ' + @strGetFields + ',ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId from ' + @TableName + ' where ' + @strWhere
END--处理页数超出范围情况 IF @PageIndex<=0 SET @PageIndex = 1--处理开始点和结束点DECLARE @StartRecord INT DECLARE @EndRecord intSET @StartRecord = (@pageIndex-1)*@PageSize + 1SET @EndRecord = @StartRecord + @PageSize - 1--继续合成sql语句SET @strSQL = @strSQL + ') as tempTable where rowId >=' + CONVERT(VARCHAR(50),@StartRecord) + ' and rowid<= ' + CONVERT(VARCHAR(50),@EndRecord)exec sp_executesql @strSQLGO

拓展:http://blog.csdn.net/jiankunking/article/details/43339301

转载于:https://www.cnblogs.com/freeliver54/p/7323672.html

[转]Sql Server 分页存储过程相关推荐

  1. SQL Server分页存储过程实践(图解)

    下面来对SQL Server分页存储过程进行一下实做.图解成功的各个步骤. 一 找到大数据量的示例表 分页都是针对大记录数的表:反之有大记录数的表,可能就需要分页.例如银行用户表,就会上千万.下面先做 ...

  2. C#调用SQL Server分页存储过程

    以SQL Server2012提供的offset ..rows fetch next ..rows only为例 e.g. 表名:Tab1 ------------------------------ ...

  3. 解决hibernate对Sql Server分页慢的问题

    解决hibernate对Sql Server分页慢的问题 参考文章: (1)解决hibernate对Sql Server分页慢的问题 (2)https://www.cnblogs.com/firstd ...

  4. 在sql server中建存储过程,如果需要参数是一个可变集合怎么处理?

    在sql server中建存储过程,如果需要参数是一个可变集合的处理 原存储过程,@objectIds 为可变参数,比如 110,98,99 ALTER PROC [dbo].[Proc_totalS ...

  5. [翻译]使用C#创建SQL Server的存储过程(Visual Studio 2005 + SQL Server 2005)

    原文地址:http://www.dotnetbips.com/articles/70eff218-3da0-4f6f-8f8d-eeea65193f2c.aspx[原文×××] [翻译]使用C#创建S ...

  6. 优化的ms sql server分页sql语句

    优化的ms sql server分页sql语句 发布时间: 2009-8-15 00:00 |  发布作者: hjh |   |  查看: 3次 特点:一次查询,数据库Databnse只返回一页的数据 ...

  7. Sql Server 中存储过程的output return的区别

    看http://zxianf.blog.163.com/blog/static/301207012009114104124969/中片关于Sql Server中存储过程output和return值的区 ...

  8. 实验8 SQL Server 的存储过程

    实验8 SQL Server 的存储过程 一.实验目的 1.掌握使用T-SQL编程的方法 2.掌握使用T-SQL语句创建一个存储过程并验证 3.掌握创建和执行带参数的存储过程 4.熟练使用系统存储过程 ...

  9. 易语言 存储过程 mysql_在易语言中调用MS SQL SERVER数据库存储过程(Transact-SQL)方法总结...

    作者:liigo 日期:2010/8/25 Microsoft SQL SERVER 数据库存储过程,根据其输入输出数据,笼统的可以分为以下几种情况或其组合:无输入,有一个或多个输入参数,无输出,直接 ...

最新文章

  1. Linux中安装配置hadoop集群
  2. 使用Codeception进行Yii2的单元测试(一)安装以及简介篇
  3. 【struts2+hibernate+spring项目实战】分页功能的完整的实现(通用分页、基类实现)
  4. YUV / RGB 格式及快速转换算法总结(转载)
  5. CodeForces - 715A Plus and Square Root(思维+构造)
  6. Android—开发过程中的相关注意事项
  7. 进军人工智能,数学基础很重要?
  8. 微型计算机使用字符编码,微型计算机系统中普遍使用的字符编码是( )
  9. ASP.NET(第七章数据插入与更新:DataList)-asp.net关注
  10. cssmatic gradient css generator,10 Best CSS Gradient Generators
  11. Android Q分区存储权限变更及适配
  12. MyBatis中出现Mapped Statements collection does not contain value 问题
  13. java默认字符串排序规则_Java 字符串排序--------请对一组字符串进行排序,字符串由大小写字母和数字组成,需要满足一下比较规则...
  14. 转!快速搭建视频直播平台
  15. Gazebo models / Gazebo Error [Node.cc:90] No namespace found
  16. linux运行proxmark3,Linux 下编译使用Proxmark3
  17. linux运行luminati,安装luminati的nodejs环境配置
  18. PyCharm 的使用(二)
  19. 公众号开发精品教程(2)——将项目接入微信及简单交互
  20. Trunk端口的配置

热门文章

  1. 以太坊是什么 - 以太坊开发入门指南
  2. 预告:大牛现身说法 TensorFlow在工程项目中的应用 | AI 研习社
  3. [链接]开方检验原理
  4. Android Studio 如何打JAR包
  5. Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.ClassVisitor
  6. Ubuntu12.04安装小记
  7. CentOS6 安装配置Smokeping
  8. Oracle八大性能视图之v$sort_usage_temp
  9. 关于Infobright的一个小TIPS
  10. android Collections.addAll()的使用