Dynamic Oracle cursor implementation

Source: Internet
Author: User

During the development process, Oracle cursors are often used for statistics (no temporary tables are required) and their statistical results are returned. However, the SQL statements behind the cursor are often dynamic, for example, select * from tablename where? Order ?. "? "Represents a condition. How can this problem be solved?

I practiced it with the help of my colleagues. Summary.

Assume that the tablename table has the following fields:

 
 
  1. field1 varchar2(50)   
  2. field2 Varchar2(50)   
  3. field3 Varchar(50)   
  4. field4 varchar2(50)   
  5. field5 varchar2(20)   
  6. field6 float,   
  7. field7 float   

1. Define a cursor

 
 
  1. create or replace package RefCursor is   
  2. -- Author : Ricky   
  3. -- Created : 2003-9-1 14:08:45   
  4. -- Purpose :   
  5. -- Public type declarations   
  6. type t_RefCursor is ref cursor;   
  7. end RefCursor; 

2. Creation type

The created type is the same as that of the table fields in tablename. Of course, it depends on whether you actually want to count all fields.

 
 
  1. create or replace type TableType as object   
  2.  (   
  3. -- Author : Ricky   
  4. -- Created : 2003-8-25 9:12:08   
  5. -- Purpose :   
  6. -- Attributes   
  7. field1 varchar2(50),   
  8. field2 Varchar2(50),   
  9. field3 Varchar(50),   
  10. field4 varchar2(50),   
  11. field5 varchar2(20),   
  12. field6 float,   
  13. field7 float   
  14.  );  

3. Create a table Type

 
 
  1. create or replace type TableTypeList as table of TableType;  

4. It is used in stored procedures or functions and is used in functions below (a table structure cannot be returned in Stored Procedures and temporary tables must be used)

 
 
  1. CREATE OR REPLACE FUNCTION "TEST" (   
  2. return TableTypeList pipelined as   
  3. begin   
  4. v_Cur RefCursor.t_Refcursor;   
  5. v_SQLStatement string(10000);   
  6. v_Table tablename%rowtype;   
  7. tmp1 tablename.field1%Type;   
  8. tmp2 tablename.field2%Type;   
  9. tmp3 tablename.field3%Type;   
  10. tmp4 tablename.field4%Type;   
  11. tmp5 tablename.field5%Type;   
  12. tmp6 tablename.field6%Type;   
  13. tmp7 tablename.field6%Type;   
  14. v_SQLStatement := 'Select * From tablename where field1='1' order by field1';   
  15. open v_Cur for v_SQLStatement;   
  16. loop   

Here is the loop Process
 

 
 
  1. fetch v_Cur into v_Comm;   
  2. exit when v_CommCur%notfound;  

Here is the statistical process you want to process. I didn't make any statistics during the intermediate process. You should add them as needed in practice.
 

 
 
  1. field1 = v_Cur.field1;   
  2. field2 = v_Cur.field2;   
  3. field3 = v_Cur.field3;   
  4. field4 = v_Cur.field4;   
  5. field5 = v_Cur.field5;   
  6. field6 = v_Cur.field6;   
  7. field7 = v_Cur.field7;   
  8. v_Table = TableType(field1,   
  9. field2,   
  10. field3,   
  11. field4,   
  12. field5,   
  13. field6,   
  14. field7)   
  15. pipe row(v_Table);   
  16. end loop   
  17. end;  

The above content is an introduction to the implementation of the dynamic form of Oracle cursor. I hope you will have some gains.

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.