Close a cursor after looping : Close Cursor « 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 » Close Cursor 
Close a cursor after looping
  
SQL>
SQL> CREATE TABLE myStudent (
  2    student_id NUMBER(5NOT NULL,
  3    department CHAR(3)   NOT NULL,
  4    course     NUMBER(3NOT NULL,
  5    grade      CHAR(1)
  6    );

Table created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10000'CS', 102'A');

row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10002'CS', 102'B');

row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10003'CS', 102'C');

row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10000'HIS', 101'A');

row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10001'HIS', 101'B');

row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10002'HIS', 101'B');

row created.

SQL>
SQL>
SQL> set serveroutput on
SQL>
SQL> CREATE OR REPLACE PROCEDURE RSLoop AS
  2    v_RSRec myStudent%ROWTYPE;
  3    CURSOR c_RSGrades IS SELECT FROM myStudent ORDER BY grade;
  4  BEGIN
  5    OPEN c_RSGrades;
  6    LOOP
  7      FETCH c_RSGrades INTO v_RSRec;
  8      EXIT WHEN c_RSGrades%NOTFOUND;
  9    END LOOP;
 10    CLOSE c_RSGrades;
 11
 12    DBMS_OUTPUT.PUT_LINE('Last row selected has ID ' || v_RSRec.student_id);
 13  END RSLoop;
 14  /

Procedure created.

SQL> show errors
No errors.
SQL>
SQL>
SQL>
SQL>
SQL> drop table myStudent;

Table dropped.

   
  
Related examples in the same category
1.Below is a function that demonstrates how to use the CLOSE statement
2.Closing the Cursor Variable
3.Close a cursor and open it again with another query
4.After the cursor is opened, the book table is dropped prior to looping through the cursor.
5.Close cursor after while loop
6.Close cursor in excpetion handler
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.