DB2 General paging Stored Procedure

Source: Internet
Author: User

Numerous online usersPaging StorageA general stored procedure is selected.DB2Page turning stored procedure in:

 
 
  1. Create procedure kyjt. USP_A_FY (in ptblname varchar (1000), -- table name
  2. In pshowname varchar (1000), -- the field name must be displayed.
  3. In pfldname varchar (1000), -- field name (primary key of the primary table, which cannot be repeated)
  4. In ppagesize integer, -- if the page size is 0, the first 10 million pieces of data are returned by default.
  5. In ppageindex integer, -- page number
  6. In pordertype integer, -- set the sorting type. If the value is not 0, the data is sorted by the primary key IN descending order)
  7. In pstrwhere varchar (1000) -- Query condition (Note: Do not add WHERE)
  8. )
  9. MODIFIES SQL DATA
  10. DETERMINISTIC
  11. LANGUAGE SQL
  12. BEGIN
  13. /**//*----------------------------------------------------------------
  14. * Copyright (C) 2006 laugh crazy
  15. * All Rights Reserved.
  16. *
  17. * Process function description:
  18. * Versatile generic paging query statement (DB2)
  19. *
  20. * Create an identifier: longping520@126.com (modification)
  21. *
  22. //-----------------------------------------------------------------------*/
  23.  
  24. DECLARE C_STRSQL VARCHAR (6000); -- subject sentence
  25. DECLARE C_STRTMP VARCHAR (100); -- Temporary Variable
  26. DECLARE C_STRORDER VARCHAR (400); -- sort type
  27.  
  28. DECLARE bill_task cursor with return to caller for S1;
  29.  
  30. -- Set pshowname = PSHOWNAME | ',' | PFLDNAME;
  31.  
  32. IF (PPAGEINDEX = 0) THEN
  33. Set ppageindex = 1;
  34. End if;
  35.  
  36. IF (PPAGESIZE = 0) THEN
  37. Set ppagesize = 10000000;
  38. End if;
  39.  
  40. -- Set sorting rules
  41. IF (PORDERTYPE <> 0) THEN
  42. SET C_STRTMP = '<(SELECT min ';
  43. SET C_STRORDER = 'ORDER BY' | PFLDNAME | 'desc ';
  44. ELSE
  45. SET C_STRTMP = '> (select max ';
  46. SET C_STRORDER = 'ORDER BY' | PFLDNAME | 'asc ';
  47. End if;
  48.  
  49. --
  50. SET C_STRSQL = 'select' | PSHOWNAME | 'from' | PTBLNAME | 'where' | PFLDNAME | ''| C_STRTMP | '(' |' TBLTMP. TTT) FROM (SELECT '| PFLDNAME | 'as TTT from' | PTBLNAME | ''| C_STRORDER | 'fetch first' | CHAR (PPAGEINDEX-1) * PPAGESIZE) | 'rows ONLY) as tbltmp) '| C_STRORDER | 'fetch FIRST' | CHAR (PPAGESIZE) | 'rows only ';
  51. --
  52. IF (PSTRWHERE <> '') THEN
  53. SET C_STRSQL = 'select' | PSHOWNAME | 'from' | PTBLNAME | 'where' | PFLDNAME | ''| C_STRTMP | '(' |' TBLTMP. TTT) FROM (SELECT '| PFLDNAME | 'as TTT from' | PTBLNAME | 'where' | PSTRWHERE | ''| C_STRORDER | 'fetch FIRST' | CHAR (PPAGEINDEX-1) * PPAGESIZE) | 'rows ONLY) as tbltmp) AND '| PSTRWHERE | ''| C_STRORDER | 'fetch FIRST' | CHAR (PPAGESIZE) | 'rows only ';
  54. End if;
  55. --
  56. IF (PPAGEINDEX = 1) THEN
  57.  
  58. SET C_STRTMP = '';
  59.  
  60. IF (PSTRWHERE <> '') THEN
  61. SET C_STRTMP = 'where' | PSTRWHERE;
  62. End if;
  63.  
  64. SET C_STRSQL = 'select' | PSHOWNAME | 'from' | PTBLNAME | ''| C_STRTMP |'' | C_STRORDER | 'fetch FIRST '| CHAR (PPAGESIZE) | 'rows only ';
  65. End if;
  66.  
  67. PREPARE S1 FROM C_STRSQL;
  68. -- OPEN C_STRSQL;
  69. -- EXECUTE S1;
  70. OPEN bill_task;
  71.  
  72. END

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.