分类目录

链接

2011年 12月
 1234
567891011
12131415161718
19202122232425
262728293031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > MySQL, SQL Server > 正文
sql分页 完整版
MySQL, SQL Server 暂无评论 阅读(2,063)
  1. -- ===================================================
  2. --        auhtor            :        数据库之家
  3. --        modified date        :        01/10/2011 13:44:15
  4. --        description        :        分页存储
  5. -- ===================================================
  6. -- ========= ======== SQL 2000========================
  7. create proc GetTablePage
  8. (
  9. @tbname        nvarchar(4000),                --查询的对象名及多表连接语句
  10. @feildcol    nvarchar(4000),                --查询的字段,多个字段用','隔开
  11. @pagesize    int,                            --每页显示的记录尺寸
  12. @pageindex    int,                            --显示的页码
  13. @strwhere    nvarchar(4000) = null,        --查询的限制条件
  14. @strorder    nvarchar(4000) = null,        --排序字段,多个字段用','隔开,注明升降序
  15. @counts        int output,                    --查询到的总的记录数
  16. @pagecounts    int output                    --按pagesize分页的总页数
  17. )
  18. as
  19. declare @strsql    nvarchar(4000)            --显示查询结果的同台sql语句
  20. declare @strtmp nvarchar(4000)                --分页总数控制sql语句
  21. declare    @pageint    int                        --每页显示的记录数
  22. declare @bitflag    bit                        --表自增列信息!
  23. declare @occsql    nvarchar(4000)            --自增列及其他列值查询sql语句
  24. declare @keycol    nvarchar(4000)            --自增列
  25.  
  26. if isnull(@feildcol,N'') = N''
  27.     set @feildcol = N'*'            --查询字段为空默认为*
  28. if isnull(@pagesize,0) < 1
  29.     set @pagesize = 10            --默认每页显示条记录(输入记录尺寸不大于时)
  30. if isnull(@pageindex,0) < 1
  31.     set @pageindex = 1            --默认显示页码为(输入页码不大于时)
  32. if isnull(@strwhere,N'') = N''        --初始化查询的限制条件
  33.     set @strwhere = N''
  34. else
  35.     set @strwhere = N' where ' + @strwhere
  36. if isnull(@strorder,N'') = N''        --初始化查询的排序方式
  37.     set @strorder = N''
  38. else
  39.     set @strorder = N' order by ' + @strorder
  40.  
  41. --计算总记录数及分页总数
  42. if @counts is null
  43. begin
  44.     set @strtmp = N' select count(*) ttt1,(count(*) + '+ltrim(@pagesize)+N' - 1)/'+ltrim(@pagesize)
  45.                     +N' ttt2 into tempcol from ' + @tbname + @strwhere
  46. exec(@strtmp)
  47. select @counts = ttt1 from tempcol
  48. select @pagecounts = ttt2 from tempcol
  49. end
  50.  
  51. --分页设置
  52. if (@pageindex <= @pagecounts)
  53. begin
  54.     set @occsql = N' select ' + @feildcol + N' into tempocctable from ' + @tbname + @strwhere + @strorder
  55.     exec(@occsql) 
  56.     select @bitflag = objectproperty(object_id('tempocctable'),'tablehasidentity')
  57.     select name INTO #tempocc
  58.     from syscolumns where id=object_id( 'tempocctable') and name!=''
  59.     if (@bitflag = 0)
  60.     begin
  61.         set @feildcol = ''
  62.         update #tempocc
  63.         set @feildcol = @feildcol + ',' + name
  64.         set @feildcol = stuff(@feildcol,1,1,'')
  65.         set @strsql = N' select identity(int,1,1) as iid,' + @feildcol + N' into resultocctable from tempocctable '
  66.     end
  67.     else
  68.     begin
  69.         select @keycol = [name] from syscolumns where id = object_id('tempocctable') 
  70.                         and columnproperty(object_id('tempocctable'),name,'isidentity') = 1
  71.         set @feildcol = ''
  72.         update #tempocc
  73.         set @feildcol = @feildcol + ',' + name where name != @keycol
  74.         set @feildcol = stuff(@feildcol,1,1,'')
  75.         set @strsql = N' select identity(int,1,1) as iid,' + @keycol + N' + 0 as ' + @keycol + N',' + @feildcol
  76.                     +N' into resultocctable from tempocctable '
  77.     end
  78.     drop table #tempocc
  79. set @strsql = @strsql +N' select top ' + ltrim(@pagesize) + N' ' + @feildcol + N' from resultocctable where iid not in (select top '
  80.                     + ltrim((@pageindex - 1)*@pagesize) + N' iid ' +N' from resultocctable)'
  81. print @strsql
  82. exec sp_executesql @strsql
  83. exec('drop table tempocctable,resultocctable,tempcol ')
  84. end
  85.  
  86. -- ========= ======== SQL 2005========================
  87. create proc getpage
  88. (
  89. @tbname        nvarchar(4000),                --查询的对象名及多表连接语句
  90. @feildcol    nvarchar(4000),                --查询的字段,多个字段用','隔开
  91. @pagesize    int,                            --每页显示的记录尺寸
  92. @pageindex    int,                            --显示的页码
  93. @strwhere    nvarchar(4000) = null,        --查询的限制条件
  94. @strorder    nvarchar(4000) = null,        --排序字段,多个字段用','隔开,注明升降序
  95. @counts        int output,                    --查询到的总的记录数
  96. @pagecounts    int output                    --按pagesize分页的总页数
  97. )
  98. as
  99. declare @strsql    nvarchar(4000)            --显示查询结果的同台sql语句
  100. declare @strtmp nvarchar(4000)                --分页总数控制sql语句
  101. declare    @pageint    int                        --每页显示的记录数
  102. declare @occsql    nvarchar(4000)            --自增列及其他列值查询sql语句
  103.  
  104. if isnull(@feildcol,N'') = N''
  105.     set @feildcol = N'*'            --查询字段为空默认为*
  106. if isnull(@pagesize,0) < 1
  107.     set @pagesize = 10            --默认每页显示条记录(输入记录尺寸不大于时)
  108. if isnull(@pageindex,0) < 1
  109.     set @pageindex = 1            --默认显示页码为(输入页码不大于时)
  110. if isnull(@strwhere,N'') = N''        --初始化查询的限制条件
  111.     set @strwhere = N''
  112. else
  113.     set @strwhere = N' where ' + @strwhere
  114. if isnull(@strorder,N'') = N''        --初始化查询的排序方式
  115.     set @strorder = N''
  116. else
  117.     set @strorder = N' order by ' + @strorder
  118.  
  119. --计算总记录数及分页总数
  120. if @counts is null
  121. begin
  122.     set @strtmp = N' select count(*) ttt1,(count(*) + '+ltrim(@pagesize)+N' - 1)/'+ltrim(@pagesize)
  123.                     +N' ttt2 into tempcol from ' + @tbname + @strwhere
  124. exec(@strtmp)
  125. select @counts = ttt1 from tempcol
  126. select @pagecounts = ttt2 from tempcol
  127. end
  128.  
  129. --分页设置
  130. if (@pageindex <= @pagecounts)
  131. begin
  132.     set @occsql = N' select row_number() over (order by getdate()) as iid,' + @feildcol + N' into tempocctable from ' 
  133.                     + @tbname + @strwhere + @strorder
  134.     exec(@occsql)
  135.     set @feildcol = stuff((select ',' + name from syscolumns where id = object_id('tempocctable') for xml path('')),1,1,'')
  136.     set @strsql = N' select top ' + ltrim(@pagesize) + N' ' + @feildcol + N' from tempocctable where iid not in (select top '
  137.                     + ltrim((@pageindex - 1)*@pagesize) + N' iid ' +N' from tempocctable)'
  138. print @strsql
  139. exec sp_executesql @strsql
  140. exec('drop table tempocctable,tempcol ')
  141. end
如果你有更好的,发上来给大家秀一下吧!
数据库之家,分享快乐~

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:sql分页 完整版 | Bruce's Blog

发表评论

留言无头像?