分页存储过程.sql 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. USE [db]
  2. GO
  3. /****** 对象: StoredProcedure [dbo].[SP_PagesA] 脚本日期: 09/16/2013 13:03:20 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. /* MSSQL数据分叶 */
  9. ALTER PROCEDURE [dbo].[SP_PagesA]
  10. (
  11. @Fields varchar(500), --要取出的字段,可以是多个表的字段,可以为空,为空表示select *
  12. @TableNames varchar(200), --表名,可以是多个表,但不能用别名
  13. @PrimaryKey varchar(100), --主键,可以为空,但@Order为空时该值不能为空
  14. @Filter varchar(500) = '', --条件,可以为空,不用填 where
  15. @Group varchar(200) = '', --分组依据,可以为空,不用填 group by
  16. @Order varchar(200) = '', --排序,可以为空,为空默认按主键升序排列,不用填 order by
  17. @PageSize int = 15, --每页记录数
  18. @CurrentPage int = 1 --当前页,0表示第1页
  19. )
  20. AS
  21. begin
  22. SET NOCOUNT ON
  23. declare @SortColumn varchar(200)
  24. declare @Operator char(2)
  25. declare @SortTable varchar(200)
  26. declare @SortName varchar(200)
  27. if @Fields = ''
  28. set @Fields = '*'
  29. if @Filter = ''
  30. set @Filter = 'where 1=1'
  31. else
  32. set @Filter = 'where ' + @Filter
  33. if @Group <>''
  34. set @Group = 'group by ' + @Group
  35. if @Order <> ''
  36. begin
  37. declare @pos1 int, @pos2 int
  38. set @Order = REPLACE(REPLACE(@Order, ' asc', ' ASC'), ' desc', ' DESC')
  39. if CHARINDEX(' DESC', @Order) > 0
  40. if CHARINDEX(' ASC', @Order) > 0
  41. begin
  42. if CHARINDEX(' DESC', @Order) < CHARINDEX(' ASC', @Order)
  43. set @Operator = '<='
  44. else
  45. set @Operator = '>='
  46. end
  47. else
  48. set @Operator = '<='
  49. else
  50. set @Operator = '>='
  51. set @SortColumn = REPLACE(REPLACE(REPLACE(@Order, ' ASC', ''), ' DESC', ''), ' ', '')
  52. set @pos1 = CHARINDEX(',', @SortColumn)
  53. if @pos1 > 0
  54. set @SortColumn = SUBSTRING(@SortColumn, 1, @pos1-1)
  55. set @pos2 = CHARINDEX('.', @SortColumn)
  56. if @pos2 > 0
  57. begin
  58. set @SortTable = SUBSTRING(@SortColumn, 1, @pos2-1)
  59. if @pos1 > 0
  60. set @SortName = SUBSTRING(@SortColumn, @pos2+1, @pos1-@pos2-1)
  61. else
  62. set @SortName = SUBSTRING(@SortColumn, @pos2+1, LEN(@SortColumn)-@pos2)
  63. end
  64. else
  65. begin
  66. set @SortTable = @TableNames
  67. set @SortName = @SortColumn
  68. end
  69. end
  70. else
  71. begin
  72. set @SortColumn = @PrimaryKey
  73. set @SortTable = @TableNames
  74. set @SortName = @SortColumn
  75. set @Order = @SortColumn
  76. set @Operator = '>='
  77. end
  78. declare @type varchar(50)
  79. declare @prec int
  80. select @type=t.name, @prec=c.prec
  81. from sysobjects o
  82. join syscolumns c on o.id=c.id
  83. join systypes t on c.xusertype=t.xusertype
  84. where o.name = @SortTable and c.name = @SortName
  85. if CHARINDEX('char', @type) > 0
  86. set @type = @type + '(' + CAST(@prec AS varchar) + ')'
  87. declare @TopRows int
  88. set @TopRows = @PageSize * @CurrentPage + 1
  89. --print @TopRows
  90. --print @Operator
  91. exec('
  92. declare @SortColumnbegin ' + @type + '
  93. set RowCount ' + @TopRows + '
  94. select @SortColumnbegin=' + @SortColumn + ' from ' + @TableNames + ' ' + @Filter + ' ' + @Group + ' order by ' + @Order + '
  95. set RowCount ' + @PageSize + '
  96. select ' + @Fields + ' from ' + @TableNames + ' ' + @Filter + ' and ' + @SortColumn + '' + @Operator + '@SortColumnbegin ' + @Group + ' order by ' + @Order + '
  97. ')
  98. SET NOCOUNT OFF
  99. end