Implement paging operations accurately in Oracle stored procedures

Source: Internet
Author: User

It can be said that the actual application of each WEB will use paging. Therefore, we need to make it more generic and efficient in actual operations. The following article is based on my personal thoughts, I used the Oracle stored procedure to create a paging Oracle stored procedure and shared it with you. I hope we can get a better solution through discussion.

The reason for using stored procedures is that you do not need to modify the program code in the future, but you only need to modify the Oracle stored procedure code. However, this example is a dynamically generated SQL statement in the stored procedure. I don't know if it will lose the features of one-time compilation and fast storage procedure. The Code is as follows:

1. First, create a package. The user creates a cursor type.

 
 
  1. create or replace package pkg_query as  
  2. type cur_query is ref cursor;  
  3. end pkg_query; 

2. Create an Oracle Stored Procedure

Create or replace procedure prc_query

(P_tableName in varchar2, -- table name

P_strWhere in varchar2, -- Query Condition

P_orderColumn in varchar2, -- Sort Columns

P_orderStyle in varchar2, -- sorting method

P_curPage in out Number, -- current page

P_pageSize in out Number, -- Number of records displayed per page

P_totalRecords out Number, -- total Number of records

P_totalPages out Number, -- total Number of pages

V_cur out pkg_query.cur_query) -- returned result set

IS

V_ SQL VARCHAR2 (1000): = ''; -- SQL statement

V_startRecord Number (4); -- Number of records that start to be displayed

V_endRecord Number (4); -- Number of records displayed after the end

BEGIN

Total number of records in a record

 
 
  1. v_sql := 'SELECT TO_NUMBER(COUNT(*)) FROM ' || p_tableName || ' WHERE 11=1';  
  2. IF p_strWhere IS NOT NULL or p_strWhere <> '' THEN  
  3. v_sql := v_sql || p_strWhere;  
  4. END IF;  
  5. EXECUTE IMMEDIATE v_sql INTO p_totalRecords; 

Verify page record size

 
 
  1. IF p_pageSize < 0 THEN  
  2. p_pageSize := 0;  
  3. END IF; 

Calculate the total number of pages based on the page size

 
 
  1. IF MOD(p_totalRecords,p_pageSize) = 0 THEN  
  2. p_totalPages := p_totalRecords / p_pageSize;  
  3. ELSE  
  4. p_totalPages := p_totalRecords / p_pageSize + 1;  
  5. END IF; 

Verification page number

 
 
  1. IF p_curPage < 1 THEN  
  2. p_curPage := 1;  
  3. END IF;  
  4. IF p_curPage > p_totalPages THEN  
  5. p_curPage := p_totalPages;  
  6. END IF; 

Implement paging Query

 
 
  1. v_startRecord := (p_curPage - 1) * p_pageSize + 1;  
  2. v_endRecord := p_curPage * p_pageSize;  
  3. v_sql := 'SELECT * FROM (SELECT A.*, rownum r FROM ' ||  
  4. '(SELECT * FROM ' || p_tableName;  
  5. IF p_strWhere IS NOT NULL or p_strWhere <> '' THEN  
  6. v_sql := v_sql || ' WHERE 11=1' || p_strWhere;  
  7. END IF;  
  8. IF p_orderColumn IS NOT NULL or p_orderColumn <> '' THEN  
  9. v_sql := v_sql || ' ORDER BY ' || p_orderColumn || ' ' || p_orderStyle;  
  10. END IF;  
  11. v_sql := v_sql || ') A WHERE rownum <= ' || v_endRecord || ') B WHERE r >= '  
  12. || v_startRecord;  
  13. DBMS_OUTPUT.put_line(v_sql);  
  14. OPEN v_cur FOR v_sql;  
  15. END prc_query; 

3. Retrieve the result set from JAVA code

 
 
  1. String sql= "{ call prc_query(?,?,?,?,?,?,?,?,?) }";  
  2. CallableStatement call = con.prepareCall(sql); 

Intermediate data settings and registration omitted

 
 
  1. call.registerOutParameter(9, OracleTypes.CURSOR); 

Retrieve result set

 
 
  1. (ResultSet) call.getObject(9); 

The above content is an introduction to how to implement paging in the Oracle stored procedure. I hope you will have some gains.

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.