SQL Server database stored procedure page

Source: Internet
Author: User
Tags rtrim
  1. Set quoted_identifier off
  2. Go
  3. Set ansi_nulls off
  4. Go
  5. Alter procedure cms_searchgetdatabypage
  6. @ Tblname varchar (255), -- table name
  7. @ Fldnames varchar (1000), -- the list of selected fields is separated by commas (,).
  8. @ Selectorderfldname varchar (500), -- Sort fields are separated by commas (,) (fields specified by keyfldname cannot be included and can be blank)
  9. @ Orderflddesc varchar (500), -- sorting field and sorting direction, such as adddate DESC and id desc (the sorting field must be specified by selectorderfldname and can be empty)
  10. @ KeyFldName varchar (255), -- primary key field
  11. @ PageSize int = 10, -- page size
  12. @ PageIndex int = 1, -- page number
  13. @ StrWhere varchar (1000) = ''-- Query condition (Note: Do not add where)
  14. AS
  15. Declare @ strWhereA varchar (1200) -- temporary variable, add where to sqlwhere
  16. Declare @ strOrderA varchar (2000) -- first sorting type
  17. Declare @ strorderb varchar (2000) -- Second sorting type
  18. Declare @ strsqla varchar (4000) -- selected for the first time
  19. Declare @ strsqlb varchar (8000) -- selected for the second time
  20. Declare @ strsql varchar (8000) -- Final selection
  21. /* Condition */
  22. If @ strwhere! =''
  23. Set @ strwherea = 'where' + @ strwhere
  24. Else
  25. Set @ strwherea =''
  26. /* Select the field list */
  27. If @ fldnames is null or rtrim (@ fldnames) =''
  28. Set @ fldnames = '*'
  29. /* List of sorting fields */
  30. If not (@ selectorderfldname is null or rtrim (@ selectorderfldname) = '')
  31. If rtrim (@ selectorderfldname) = 'id'
  32. Set @ selectorderfldname =''
  33. Else
  34. Set @ selectorderfldname = ',' + @ selectorderfldname
  35. /* Construct an order and sort by the specified Method */
  36. If @ orderFldDesc is null or rtrim (@ orderFldDesc) =''
  37. Set @ orderFldDesc = 'order by id desc'
  38. Else
  39. Set @ orderFldDesc = 'ORDER BY' + @ orderFldDesc
  40. Set @ strOrderA = UPPER (@ orderFldDesc)
  41. Set @ strOrderB = replace (@ strOrderA, 'desc', 'desc1 ')
  42. Set @ strOrderB = replace (@ strOrderB, 'asc ', 'desc ')
  43. Set @ strOrderB = replace (@ strOrderB, 'sc1', 'asc ')
  44. /* Page 1 */
  45. If @ PageIndex = 1
  46. Set @ strSQL = 'select top '+ str (@ PageSize) + ''+ @ fldNames +' from ['+ @ tblName +'] with (nolock) '+ @ strWhereA + ''+ @ strOrderA
  47. Else
  48. Begin
  49. -- Retrieve the total number of records
  50. Declare @ SQL nvarchar (500)
  51. Declare @ maxCount int
  52. Declare @ maxPage int
  53. Declare @ tempRowCount int
  54. Set @ SQL = 'select @ maxcount = count ('+ @ keyfldname +') from ['+ @ tblname +'] '+ @ strwherea
  55. Exec sp_executesql @ SQL, n' @ maxcount int output', @ maxcount output
  56. Set @ maxpage = @ maxcount/@ pagesize
  57. If (@ maxcount % @ pagesize> 0)
  58. Set @ maxpage = @ maxpage + 1
  59. /* Last page */
  60. If @ pageindex >=@ maxpage
  61. Begin
  62. Set @ PageIndex = @ maxPage
  63. Set @ strSqlA = char (13) + '(select top' + str (@ maxCount % @ PageSize) + ''+ @ keyFldName + @ selectOrderFldName + 'from [' + @ tblName + '] as a with (nolock)' + @ strWhereA + @ strOrderB + ') '+ char (13)
  64. Set @ strSqlB = char (13) + '(select' + @ keyFldName + 'from' + @ strSqlA + 'as B) '+ char (13)
  65. Set @ strSQL = 'select' + @ fldNames + 'from [' + @ tblName + '] where ([' + @ keyFldName + '] in' + @ strSqlB + ') '+ @ strOrderA + char (13)
  66. End
  67. Else
  68. Begin
  69. /* Not the first page or the last page */
  70. If (@ PageIndex <= @ maxPage/2)
  71. Begin
  72. -- First Half of the pages
  73. Set @ tempRowCount = @ PageIndex * @ PageSize
  74. /* Construct an SQL statement. This paging algorithm aims to achieve efficient sorting of non-primary keys. By tony */
  75. /* 1. Select perPage * pageNum records in descending order based on the specified field + primary key field */
  76. Set @ strSqlA = char (13) + '(select top' + str (@ tempRowCount) + ''+ @ keyFldName + @ selectOrderFldName + 'from [' + @ tblName + '] as a with (nolock)' + @ strWhereA + @ strOrderA + ') '+ char (13)
  77. /* 2. Select perPage records from the selected records in ascending order */
  78. Set @ strSqlB = char (13) + '(select top' + str (@ PageSize) + ''+ @ keyFldName + 'from' + @ strSqlA + 'as B '+ @ strOrderB +') '+ char (13)
  79. /* 3. Select the records of the primary key in the record selected for the second time from the database, sort them in descending order, and complete the query by page */
  80. Set @ strSQL = 'select' + @ fldNames + 'from [' + @ tblName + '] where ([' + @ keyFldName + '] in' + @ strSqlB + ') '+ @ strOrderA
  81. End
  82. Else
  83. Begin
  84. -- The last half of the pages
  85. Set @ temprowcount = @ maxcount-(@ pageindex-1) * @ pagesize
  86. /* Construct an SQL statement. This paging algorithm aims to achieve efficient sorting of non-primary keys. By Tony */
  87. /* 1. Select perpage * pagenum records in descending order based on the specified field + primary key field */
  88. Set @ strSqlA = char (13) + '(select top' + str (@ tempRowCount) + ''+ @ keyFldName + @ selectOrderFldName + 'from [' + @ tblName + '] as a with (nolock)' + @ strWhereA + @ strOrderB + ') '+ char (13)
  89. /* 2. Select perPage records from the selected records in ascending order */
  90. Set @ strSqlB = char (13) + '(select top' + str (@ PageSize) + ''+ @ keyFldName + 'from' + @ strSqlA + 'as B '+ @ strOrderA +') '+ char (13)
  91. /* 3. Select the records of the primary key in the record selected for the second time from the database, sort them in descending order, and complete the query by page */
  92. Set @ strSQL = 'select' + @ fldNames + 'from [' + @ tblName + '] where ([' + @ keyFldName + '] in' + @ strSqlB + ') '+ @ strOrderA
  93. End
  94. End
  95. End
  96. Set nocount on
  97. /* Print @ strSQL */-- display SQL
  98. Exec (@ strSQL)
  99. Set nocount off
  100. RETURN
  101. GO
  102. Set quoted_identifier off
  103. Go
  104. Set ansi_nulls on
  105. Go
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.