Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
PL/SQL CASE Statement
Next article icon

Implicit Statement Results in PL/SQL

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Implicit statement results in PL/SQL refer to returning the results of a query automatically from a PL/SQL block, procedure, or function without explicitly using cursors or OUT parameters.

Implicit Statement Results, introduced in Oracle 12c, helps developers simplify their code by returning query results directly for them to work with other applications or even client-side APIs. This article will illustrate implicit statement results in detail with necessary syntax, examples, and outputs.

Implicit Statement Results in PL/SQL

Implicit statement results in PL/SQL provide a way to return query results directly from a PL/SQL block, procedure, or function without the need for explicit cursors or OUT parameters. This feature, introduced in Oracle 12c, simplifies the process of handling query results and enhances code readability and maintainability.

By using implicit statement results, developers can streamline their code and efficiently pass query results to applications or client-side APIs.

Syntax:

DECLARE
v_cursor SYS_REFCURSOR;
BEGIN
-- Open a cursor for a query
OPEN v_cursor FOR <SELECT query>;

-- Return the result set using DBMS_SQL.RETURN_RESULT
DBMS_SQL.RETURN_RESULT(v_cursor);
END;
/

Explanation:

  • v_cursor: A SYS_REFCURSOR variable that holds the result of the query.
  • OPEN v_cursor FOR <SELECT query>: Opens the cursor and executes the query, storing the result set in the cursor.
  • DBMS_SQL.RETURN_RESULT(v_cursor): Automatically returns the result set of the query to the client application without explicit fetching.

Syntax with Multiple Result Sets

DECLARE
v_cursor1 SYS_REFCURSOR;
v_cursor2 SYS_REFCURSOR;
BEGIN
-- Open first cursor for the first query
OPEN v_cursor1 FOR SELECT * FROM employees;
DBMS_SQL.RETURN_RESULT(v_cursor1);

-- Open second cursor for the second query
OPEN v_cursor2 FOR SELECT * FROM departments;
DBMS_SQL.RETURN_RESULT(v_cursor2);
END;
/

Explanation:

v_cursor1 and v_cursor2: Each SYS_REFCURSOR stores the result of a separate query.

Both cursors are returned sequentially, with the result sets sent back to the client.

Examples of Implicit Statement Results in PL/SQL

The PL/SQL block demonstrates how to return a single result set implicitly. Within the FOR loop, the query selects employee_id, first_name, and last_name from the employees table where department_id is 10.

Returning a Single Result Set

The DBMS_SQL.RETURN_RESULT procedure sends this result set directly to the client tool, such as SQL*Plus or SQL Developer, which automatically displays the results without needing to explicitly handle cursors.

Query:

BEGIN
-- Returning result set implicitly
FOR result IN (SELECT employee_id, first_name, last_name FROM employees WHERE department_id = 10)
LOOP
-- Output each row (SQL*Plus or similar tool will automatically display the result set)
DBMS_SQL.RETURN_RESULT(result);
END LOOP;
END;
/

Output:

EMPLOYEE_ID

FIRST_NAME

LAST_NAME

101

John

Doe

102

Jane

Smith

103

Michael

Johnson

Explanation:

The PL/SQL below executes a query within the FOR loop for the employee_id, first_name, and last_name of employees in the employees table whose department_id is 10. By default, DBMS_SQL.RETURN_RESULT sends the result set to the client tool through SQL*Plus or SQL Developer and displays it without explicitly defining and opening a cursor.

Returning Multiple Result Sets

This PL/SQL block demonstrates how to return multiple result sets within a single block. It first retrieves and returns employee data for department_id 20, followed by returning department data for location_id 1700.

The DBMS_SQL.RETURN_RESULT procedure is called twice—once for each query—to send both result sets sequentially to the client tool for display.

Query:

BEGIN
-- First result set
FOR result IN (SELECT employee_id, first_name, last_name FROM employees WHERE department_id = 20)
LOOP
DBMS_SQL.RETURN_RESULT(result);
END LOOP;

-- Second result set
FOR result IN (SELECT department_id, department_name FROM departments WHERE location_id = 1700)
LOOP
DBMS_SQL.RETURN_RESULT(result);
END LOOP;
END;
/

Output:

EMPLOYEE_ID

FIRST_NAME

LAST_NAME

201

Alice

Brown

202

Bob

White

DEPARTMENT_ID

DEPARTMENT_NAME

10

Administration

20

Sales

Explanation:

  • This PL/SQL block uses implicit statement results to return two result sets.
  • The first result set returns the employees from the employees table whose department_id is 20, fetching into employee_id, first_name, and last_name, returning the result via DBMS_SQL.RETURN_RESULT.
  • Then it selects the department_id and department_name from the departments table for location_id 1700 and returns this second result set in the same way. The client tool will then display both result sets, one after the other.

Using the get_next_result() procedure

The get_next_result() procedure can be conceptually viewed as one that iterates through the various result sets returned by a single execution in those databases supporting multiple result sets, like Oracle with dynamic SQL and some APIs.

Though the latter is not available in Oracle PL/SQL, the same could be emulated using cursors and dynamic SQL.

Query:

DECLARE
-- Define cursors for multiple result sets
CURSOR c1 IS
SELECT employee_id, first_name FROM employees;

CURSOR c2 IS
SELECT department_id, department_name FROM departments;

-- Record types for fetching results
emp_record c1%ROWTYPE;
dept_record c2%ROWTYPE;

BEGIN
-- Open and process the first cursor
OPEN c1;
LOOP
FETCH c1 INTO emp_record;
EXIT WHEN c1%NOTFOUND;
-- Process each row from the first result set
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_record.employee_id);
DBMS_OUTPUT.PUT_LINE('First Name: ' || emp_record.first_name);
END LOOP;
CLOSE c1;

-- Open and process the second cursor
OPEN c2;
LOOP
FETCH c2 INTO dept_record;
EXIT WHEN c2%NOTFOUND;
-- Process each row from the second result set
DBMS_OUTPUT.PUT_LINE('Department ID: ' || dept_record.department_id);
DBMS_OUTPUT.PUT_LINE('Department Name: ' || dept_record.department_name);
END LOOP;
CLOSE c2;

END;
/

Output:

employee_id

first_name

1

John

2

Jane

department_id

department_name

10

HR

20

IT

Explanation:

  • The following PL/SQL block demonstrates how to handle more than one result set through cursors. Two cursors are initialized inside
  • c1 returns the employee_id and first_name from the employees table c2 returns the department_id and department_name from the departments table Both of them have an associated record type
  • emp_record in case of c1 and dept_record in case of c2 for storing the retrieved data.
  • BEGIN In the BEGIN section, the code opens cursor c1 and fetches each row in order, printing the details of the employee, including ID and first name.
  • Once all the rows are processed, it closes the cursor.

Using Implicit Statement Results in PL/SQL Blocks

Next, we will illustrate - through examples and explanations - how to work with implicit statement results within PL/SQL blocks.Implicit statement results can be returned from an anonymous PL/SQL block.

To do this, you need to create a cursor for your query and then use the DBMS_SQL.RETURN_RESULT to return the results.

Query:

BEGIN
DECLARE
v_cursor SYS_REFCURSOR;
BEGIN
-- Open a cursor for the SELECT query
OPEN v_cursor FOR SELECT employee_id, first_name, last_name FROM employees;

-- Return the result set to the client
DBMS_SQL.RETURN_RESULT(v_cursor);
END;
END;
/

Output:

EMPLOYEE_IDFIRST_NAMELAST_NAME
1JohnDoe
2JaneSmith
3MichaelJohnson

Explanation:

  • This PL/SQL block uses SYS_REFCURSOR to return two result sets. The first query opens a cursor, v_cursor1, that selects all rows in the employees table. Its result is then returned using DBMS_SQL.RETURN_RESULT.
  • The second query opens another cursor, v_cursor2, for selecting all rows from the departments table, and its result is also returned. Both result sets will return and appear consecutively in the client tool, say, SQL*Plus or SQL Developer.
  • In this way, more than one result set can return under a single PL/SQL block.

Handling Multiple Result Sets

Returning Multiple Result Sets You can return more than one result set in a single PL/SQL block by calling DBMS_SQL.RETURN_RESULT more than once. Each result set is returned in succession.

Query:

BEGIN
DECLARE
v_cursor1 SYS_REFCURSOR;
v_cursor2 SYS_REFCURSOR;
BEGIN
-- Open first cursor for employees
OPEN v_cursor1 FOR SELECT employee_id, first_name FROM employees;
DBMS_SQL.RETURN_RESULT(v_cursor1);

-- Open second cursor for departments
OPEN v_cursor2 FOR SELECT department_id, department_name FROM departments;
DBMS_SQL.RETURN_RESULT(v_cursor2);
END;
END;
/

Explanation:

Multiple Cursors: Two SYS_REFCURSOR variables (v_cursor1 and v_cursor2) are used to hold the results of two separate queries.

Sequential Return: Each result set is returned in sequence using DBMS_SQL.RETURN_RESULT.

Conclusion

Implicit statement results in PL/SQL make it a lot easier and faster to return query results from PL/SQL blocks, procedures, and functions. This is because you don't have the overhead of explicit cursors and OUT parameters-and most importantly, it cleans up your code and is more efficient.

This comes in handy if you are working with applications that require multiple result sets, such as reporting tools or client applications using JDBC or OCI. Implicit statement results improve code readability and reduces complexity. Implicit statement results provide a complete modern approach toward returning data from PL/SQL blocks in Oracle 12c onwards.


Next Article
PL/SQL CASE Statement

B

badalmishra28
Improve
Article Tags :
  • Databases
  • PL/SQL

Similar Reads

  • PL/SQL CONTINUE Statement
    PL/SQL is a block-structured language that enables developers to combine the power of SQL with procedural statements. All the statements of a block are passed to the Oracle engine all at once which increases processing speed and decreases the traffic. A key component of loop control in PL/SQL is the
    4 min read
  • What is Nested Select Statement in PL/SQL?
    PL/SQL is a Procedural Language/Structured Query Language and it enables users to write procedural logic directly within the database, including procedures, functions, triggers, and packages. In this article, we will understand Nested select statements in PL/SQL along with the examples and so on. Un
    4 min read
  • PL/SQL INSERT Statement
    The PL/SQL INSERT statement is vital for adding new records to a database table. By specifying the table's name and providing values for its columns, users can populate their database with essential information. This functionality enables efficient data entry and ensures the completeness of datasets
    3 min read
  • PL/SQL CASE Statement
    PL/SQL stands for Procedural Language Extension to the Structured Query Language and it is designed specifically for Oracle databases it extends Structured Query Language (SQL) capabilities by allowing the creation of stored procedures, functions, and triggers. The PL/SQL CASE statement is a powerfu
    4 min read
  • PL/SQL GOTO Statement
    PL/SQL also known as Procedural Language/Structured Query Language, PL/SQL is a powerful programming language used in Oracle databases to do interaction with data. One of its features is the GOTO statement, which helps control how a program flows by allowing it to jump to specific statements within
    5 min read
  • PL/SQL NULL Statement
    PL/SQL, the Procedural Language/Structured Query Language, is a database programming language utilized for database management in Oracle. Within this language, the NULL statement is crucial in enhancing code readability and functionality. This article aims to look into the significance of the NULL s
    5 min read
  • PostgreSQL - IF Statement
    PostgreSQL IF statement is an essential tool for implementing conditional logic within SQL queries and stored procedures. It allows developers to execute different actions based on specific conditions and enhances the flexibility of database operations. In this article, we will explore various Postg
    5 min read
  • SQL CREATE VIEW Statement
    The SQL CREATE VIEW statement is a very powerful feature in RDBMSs that allows users to create virtual tables based on the result set of a SQL query. Unlike regular tables, these views do not store data themselves rather they provide a way of dynamically retrieving and presenting data from one or ma
    4 min read
  • PL/SQL SELECT INTO Existing Table
    PL/SQL is a programming language that is used alongside SQL for writing procedural code such as stored procedures, functions, triggers, and packages within the Oracle Database. It was developed by Oracle Corporation and is widely used in database programming. PL/SQL is a programming language that ha
    5 min read
  • PL/SQL CREATE TABLE Statement
    PL/SQL CREATE TABLE statement is a fundamental aspect of database design and allows users to define the structure of new tables, including columns, data types, and constraints. This statement is crucial in organizing data effectively within a database and providing a blueprint for how data should be
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences