Open cursor from a dynamic statement : Cursor Open « Cursor « Oracle PL / SQL

Home
Oracle PL / SQL
1.Aggregate Functions
2.Analytical Functions
3.Char Functions
4.Constraints
5.Conversion Functions
6.Cursor
7.Data Type
8.Date Timezone
9.Hierarchical Query
10.Index
11.Insert Delete Update
12.Large Objects
13.Numeric Math Functions
14.Object Oriented Database
15.PL SQL
16.Regular Expressions
17.Report Column Page
18.Result Set
19.Select Query
20.Sequence
21.SQL Plus
22.Stored Procedure Function
23.Subquery
24.System Packages
25.System Tables Views
26.Table
27.Table Joins
28.Trigger
29.User Previliege
30.View
31.XML
Oracle PL / SQL » Cursor » Cursor Open 
Open cursor from a dynamic statement
 
SQL>
SQL> CREATE TABLE MyTable(yourRow INTEGER, yourDesc VARCHAR2(50));

Table created.

SQL>
SQL> SET ECHO ON
SQL> SET SERVEROUTPUT ON
SQL>
SQL> DECLARE
  2      TYPE your_cursor_type IS REF CURSOR;
  3      your_cursor  your_cursor_type;
  4
  5      TYPE dyn_record IS RECORD (
  6          yourrow    INTEGER,
  7          yourdesc   VARCHAR2(50)
  8          );
  9
 10      dyn_rec dyn_record;
 11
 12      dynamic_select_stmt VARCHAR2(100);
 13  BEGIN
 14      dynamic_select_stmt := 'SELECT yourrow, yourdesc FROM mytable';
 15      dynamic_select_stmt := dynamic_select_stmt || ' ORDER BY yourrow DESC';
 16
 17      OPEN your_cursor FOR dynamic_select_stmt;
 18
 19      LOOP
 20          FETCH your_cursor
 21              INTO dyn_rec;
 22          EXIT WHEN your_cursor%NOTFOUND;
 23
 24          DBMS_OUTPUT.PUT_LINE(dyn_rec.yourrow || ' ' || dyn_rec.yourdesc);
 25      END LOOP;
 26      CLOSE your_cursor;
 27  END;
 28  /

PL/SQL procedure successfully completed.

SQL>
SQL> drop table mytable;

Table dropped.

SQL>
SQL> --

 
Related examples in the same category
1.Cursor OPEN Example
2.Open a cursor for read
3.Open a cursor based on the result of row_number over partition by, order by
4.ORA-6511 error: cursor already open
5.An illegal location for a cursor.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.