环思产品论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 2373|回复: 0
打印 上一主题 下一主题

报表每页固定行数使用SQL分页示例

[复制链接]

15

主题

19

帖子

277

积分

超级版主

大周

Rank: 8Rank: 8

积分
277
QQ
跳转到指定楼层
楼主
发表于 2014-12-11 11:04:23 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
客户要求效果


使用SQL分页

  1. DECLARE @iPageCount INT = 8 /*每页8条*/



  2. CREATE TABLE #tmpIndex (iIndex INT)
  3. DECLARE @i INT = 1
  4. WHILE @i <=@iPageCount
  5. BEGIN
  6. INSERT #tmpIndex (iIndex) VALUES(@I) ;
  7. SET @I = @I+1
  8. END


  9. SELECT sMaterialNo,ummMaterialGUID
  10. INTO #tmpHdr
  11. FROM dbo.mmMaterialFabric a
  12. WHERE a.sMaterialNo IN
  13. (
  14. 'S141009004AR1'
  15. ,'S141013007D'
  16. )

  17. SELECT A.ummMaterialGUID,b.sYarnNo,sYarnName=c.sMaterialNo,b.sUsage
  18. ,iOrder=ROW_NUMBER() OVER(PARTITION BY B.utmBOMHdrGUID,B.sUsage ORDER BY b.sYarnNo)
  19. ,iIndex=CONVERT(INT,0)
  20. ,iPage=CONVERT(INT,0)
  21. INTO #tmpBom
  22. FROM dbo.tmBOMHdr a
  23. JOIN dbo.tmBOMDtl b ON b.utmBOMHdrGUID = a.uGUID
  24. JOIN dbo.mmMaterialYarn c ON c.ummMaterialGUID = b.ummMaterialGUID
  25. WHERE a.ummMaterialGUID IN
  26. (
  27. SELECT ummMaterialGUID FROM #tmpHdr
  28. )

  29. UPDATE #tmpBom SET iIndex = (iOrder - 1) % @iPageCount + 1,iPage = (iOrder - 1) / @iPageCount + 1

  30. SELECT DISTINCT ummMaterialGUID,iPage
  31. INTO #tmpPage
  32. FROM #tmpBom

  33. SELECT a.sMaterialNo,a.ummMaterialGUID,b.iPage,c.iIndex
  34. INTO #tmpTotal
  35. FROM #tmpHdr a
  36. JOIN #tmpPage b ON b.ummMaterialGUID = a.ummMaterialGUID,#tmpIndex c

  37. SELECT A.sMaterialNo,a.iPage,a.iIndex
  38. ,sYarnNoJ=B.sYarnNo,sYarnNameJ=B.sYarnName
  39. ,sYarnNoW=C.sYarnNo,sYarnNameW=C.sYarnName
  40. FROM #tmpTotal A
  41. LEFT JOIN #tmpBom B ON B.ummMaterialGUID = A.ummMaterialGUID AND b.iPage = a.iPage AND b.iIndex = a.iIndex AND B.sUsage = 'J'
  42. LEFT JOIN #tmpBom C ON C.ummMaterialGUID = A.ummMaterialGUID AND c.iPage = a.iPage AND c.iIndex = a.iIndex AND C.sUsage = 'W'
  43. ORDER BY sMaterialNo,iPage,iIndex

  44. DROP TABLE #tmpIndex
  45. DROP TABLE #tmpHdr
  46. DROP TABLE #tmpBom
  47. DROP TABLE #tmpPage
  48. DROP TABLE #tmpTotal
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|上海环思 ( 沪ICP备11022428号 )

GMT+8, 2024-11-9 10:08 , Processed in 0.080115 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表