EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

FETCH in Oracle

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » Oracle Tutorial » FETCH in Oracle

FETCH in Oracle

Introduction to FETCH in Oracle

FETCH statement is used in Oracle to retrieve row or rows of data from the result set of a multi row query as FETCH statement has the ability to retrieve one row of data at a instance, more than one row of data or even all rows of data present in the result set thereby allowing the developer to control the percentage of data from the actual result set is required and then storing the row/ rows of data retrieved from FETCH into corresponding columns selected from query.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

FETCH cursorName INTO variableList;

Parameters:

  • cursorName: It refers the name of the cursor from where we want to fetch the rows.
  • variableList: It refers to the name of the variables in which we want to store the rows from the cursor. The variables should be comma delimited while specifying.

How does FETCH Statement work in Oracle?

  • FETCH statement works in oracle as it is used to retrieve the rows from cursor so that the developer can do some sort of operation on it or store it into a corresponding variable.
  •  So, FETCH is generally used in PL/SQL with cursor and also in SQL.
  • The cursor is used to retrieve the result set from the database based on the SELECT statement and the conditions present in the SELECT statement.
  • So once the cursor retrieves the result set which is a combination of rows and columns, we need to do some operation on the result set. So, in that case we use the FETCH statement to retrieve those rows from cursor.
  • The FETCH statement allows us to control the number of rows we want to work or operate. We can retrieve one row, more than one or all the rows from the cursor.
  • In case of SQL query or statement we write it at the end of the query, So after the query executes (with or without WHERE clause) and we get the result set based on the query.
  • The FETCH clause present at the end helps us to limit the number of rows of data actually we want to retrieve and display as an output and we can change it as per our use.

Examples of FETCH in Oracle

Below are the examples mentioned:

Example #1

Finding the Age of the Employee using a Function.

In this example we are going to use FETCH statement inside a function to find the age of the concerned employee. The function would take the employee_id of the employee as a variable and would return the age of the employee if present or return a error number if the employee is not present.

Code:

PL/SQL Function

CREATE OR REPLACE Function FindAge
( employee_id IN varchar2 )
RETURN number
IS
v_age number;
CURSOR c1
IS
SELECT age
FROM employee
WHERE employee_id = employee_id;
BEGIN
OPEN c1;
FETCH c1 INTO v_age;
if c1%notfound then
v_age := 9999;
end if;
CLOSE c1;
RETURN v_age;
END;

If we see the above PL/SQL program, the function with name Find Age is created which takes the employee_id as a parameter in the function. The cursor is then initialized and it retrieves the age of the employee based on the employee_id the developer gave as an argument while calling the function. The FETCH statement if we see in the program then retrieves the age from the cursor and stores it in the age variable which is a local variable we declared at the beginning of the function. We then return the age or an error number in case there is no employee present in the table with the input employee id.

Output:

Let us now run the program in SQL developer and check the result.

fetch in oracle 1

As we can see in the screenshot above the function has been compiled with no errors. Let us now call the function and execute it in the SQL developer.

The below program calls the function and then displays the age of the employee.

Popular Course in this category
Sale
Oracle Training (14 Courses, 8+ Projects)14 Online Courses | 8 Hands-on Projects | 120+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (9,217 ratings)
Course Price

View Course

Related Courses
Oracle DBA Database Management System Training (2 Courses)All in One Financial Analyst Bundle - 250+ Courses, 40+ Projects

Code:

SET SERVEROUTPUT ON
DECLARE
v_age NUMBER := 0;
BEGIN
v_age := FindAge('AD004');
DBMS_OUTPUT.PUT_LINE('Age is: ' || v_age);
END;

Output:

Let us now run the above statements in SQL Developer and check the result.

fetch in oracle 2

As we can see in the above screenshot the age of the employee gets displayed in the output.

Example #2

FETCH in SQL to limit the number of Rows.

In this example we will check the use of FETCH clause to limit the number of rows to be retrieved from database. So, since we have a table called employee in the database we will retrieve only five rows from employee table of employees having age less than 40 years.

Code:

SQL Query

SELECT * FROM EMPLOYEE WHERE AGE<40
FETCH NEXT 5 ROWS ONLY;

So, the above query should fetch only five rows from the employee table which satisfies the condition. Let us run the query in SQL developer.

Output:

Example #2

As we can see in the above screenshot only five rows satisfying the condition (age less than 40) is displayed.

Advantages of using FETCH in Oracle

  • FETCH is used to limit the number of rows we want to retrieve from the database or cursor in case of PL/SQL.
  • It is important as it allows us to limit the number of rows Suppose if we use simple WHERE clause then it will return all the rows but in case of FETCH we can set a limit of rows and based on that the Oracle database will return the rows.

Conclusion

In this article we discussed about FETCH in oracle. We discussed the syntax and its parameters in this article along with the working of FETCH in Oracle. Later on we discussed two examples from PL/SQL and SQL.

Recommended Articles

This is a guide to FETCH in Oracle. Here we discuss the introduction, how does FETCH statement work in Oracle with examples and advantages. You may also have a look at the following articles to learn more –

  1. Oracle Alter Table
  2. Oracle FOREIGN Key
  3. Oracle Full Outer Join
  4. BETWEEN in Oracle

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Oracle Tutorial
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • 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 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

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

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

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
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.

Let’s Get Started

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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

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

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.

Special Offer - Oracle Course Training Learn More