EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial Cursor in Oracle
Secondary Sidebar
Oracle Tutorial
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Golden Gate
    • Oracle Virtual Machine
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle?Primavera
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle ERP
    • Oracle ASM
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

Cursor in Oracle

By Priya PedamkarPriya Pedamkar

Cursor in Oracle

Introduction to Cursor in Oracle

Cursors are very important while writing PL/SQL queries. In this article, we are going to discuss about Cursors in Oracle. So basically Cursor is a pointer to a context area. The context area is basically a memory space for processing an SQL statement. Thus context area contains all information which is required to process the statement. So, basically the cursor points to this particular area. On executing the statement the cursor holds the returned result set which can contain single or multiple rows. As we go on we will learn about cursor in more detail.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,992 ratings)

Cursor Types and Syntax

There are two types of cursors in Oracle. Implicit Cursors and Explicit Cursors.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

two types of cursors

1. Implicit Cursors

As the name suggests implicit cursors are created by oracle. Whenever an SQL statement is executed implicit cursor is created. DML statements like UPDATE, INSERT and DELETE automatically creates implicit cursor. Incase of UPDATE and INSERT DML statements the cursor holds the information of the rows to be affected and in case of INSERT it holds the information of the data which is to be inserted.

The whole execution of the implicit cursor is managed internally by oracle and it reveals the cursor status through certain attributes like %ROWCOUNT, %ISOPEN, %ISFOUND.

  • %ROWCOUNT: It tells how many rows are returned by the cursor.
  • %ISOPEN: This attribute returns Boolean which means TRUE if the cursor is open or else FALSE.
  • %FOUND: This attribute also returns Boolean. TRUE if successful fetch has been executed and FALSE if there is unsuccessful fetch (no row returned).
  • r%NOTFOUND: This attribute returns opposite of the %FOUND attribute. FALSE if the row is fetched and TRUE if no row is fetched.

2. Explicit Cursors

These cursors are created by the users in the oracle database. In this type of cursor, the programmers are allowed to create their own context area. In explicit cursor, the programmer gets their own context area to get more control to execute their DML statements. The explicit cursors are declared in the SELECT statement of the oracle database.

Syntax

CURSOR <cursorname> IS <select_statement>;
<cursorvariable declaration>
BEGIN
OPEN <cursorname>;
FETCH <cursorname> INTO <cursorvariable>;
CLOSE
END;

Parameters:

  • cursor_name: It is used to give the name of the cursor.
  • select_statement: Here we give the select query which returns single or multiple rows.

Cursor Action in Oracle

There are four actions that we need to use explicit cursors.

  • DECLARE: In this section, the programmer actually creates a named context area for the Select statement. The select statement is declared in this section of the PL/SQL program.
  • OPEN: In this section oracle actually allocates memory for the cursor.
  • FETCH: In this section actual execution starts. The select statement fetches the records from the database and stores it in the allocated memory. The data is fetched record by record way. It is a record level activity. So the set of records is also called Active Set.
  • CLOSE: This statement is used to close the cursor and the memory allocated to the cursor will be released.

Example of Cursor in Oracle

In this example, we are going to get the employee id from the employee table only for those employees who are from the city of Mumbai. We are going to use Cursor for this situation. Let us look at the PL/SQL block for displaying the id of the employees based in Mumbai.

Code:

PL/SQL BLOCK
SET SERVEROUTPUT ON;
DECLARE
v_empid varchar2(20);
CURSOR emp_cur IS
SELECT employee_id from employee WHERE CITY ='Mumbai';
BEGIN
OPEN emp_cur;
LOOP
FETCH emp_cur INTO v_empid;
EXIT WHEN emp_cur%NOTFOUND;
dbms_output.put_line('The employee id is' || v_empid);
END LOOP;
CLOSE emp_cur;
END;

The SET server output is used to allow the DBMS output command to print the messages in SQL Developer. Since we are going to use it for our execution. In the above PL/SQL block we have the first DECLARE section in which we declare the cursor and the cursor variable. In the next, we have the execution section where we OPEN the cursor which means the memory space has been allocated. The FETCH is used to execute the SELECT query and then store it in the cursor variable which we had declared.

We will use LOOP to transverse the records in the whole table and dbms_output.put_line statement to print the record after every iteration. The attribute %NOTFOUND is used to exit the LOOP when there are no records. The CLOSE cursor statement is used to close the cursor and release the memory. The END statement at the end is used at last to end the PL/SQL block. One important point to remember is that we need to close the cursor before the end statement and the LOOP should end before closing the cursor.

Let us now execute the PL/SQL cursor block in SQL developer and the below screenshot shows us the output after executing the block successfully.

cursor in oracle 1

The output shows the records. In this case, only three records are from Mumbai.

Advantages and Disadvantages of Cursor in Oracle

Given below are the advantages and disadvantages:

Advantages

  • Cursor fetches the data Row wise so Row Wise validation is possible using the cursor.
  • It retrieves one row at a time, unlike SQL select query which selects all rows at a time.
  • It is faster than While Loop but does have more overhead.
  • It provides traversals over records of the database.

Disadvantages

  • They decrease the performance of inserts, updates, and deletes.
  • They also take up space which depends on the number of fields used.

Conclusion

In the article, we discussed what a cursor really is in the introduction part and then the different types of cursors along with their attributes and syntax. We got to see the working of the cursor through example and as well as the advantages and disadvantages of using the cursor.

Recommended Articles

This has been a guide to Cursor in Oracle. Here we discuss the introduction, cursor types, advantages, disadvantages, and example of cursor in oracle. You may also have a look at the following articles to learn more –

  1. Oracle Triggers
  2. Cursors in PL/SQL
  3. Oracle Clauses
  4. Oracle Versions
Popular Course in this category
Oracle Training (14 Courses, 8+ Projects)
  14 Online Courses |  8 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle DBA Database Management System Training (2 Courses)4.9
All in One Financial Analyst Bundle- 250+ Courses, 40+ Projects4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more