SlideShare una empresa de Scribd logo
1 de 10
Cursors In Oracle
Suhel Firdus
Definition
• A cursor is a temporary work area created in
the system memory when a SQL statement is
executed. A cursor contains information on a
select statement and the rows of data
accessed by it.
• Can hold information of more than one row.
• One row at a time can be accessed.
Cursor types
• Implicit Cursor
• Explicit Cursor
Implicit Cursor
• Oracle internally creates Implicit cursor
• Whenever a DML operation like
SELECT,UPDATE,INSERT happens on a row
Oracle creates the cursor internally.
• SELECT on a single row is a implicit cursor.
• Status of the Implicit cursor can be found
using SQL%.
• Use cannot specify a name to Implicit Cursor.
Implicit Cursor
SQL%FOUND The return value is TRUE, if the DML
statements like INSERT, DELETE and
UPDATE affect at least one row and if
SELECT ….INTO statement return at least
one row.
SQL%ROWCOUNT The return value is FALSE, if DML
statements like INSERT, DELETE and
UPDATE at least one row and if SELECT
….INTO statement return at least one row
SQL%NOTFOUND Return the number of rows affected by
the DML operations INSERT, DELETE,
UPDATE, SELECT
Explicit Cursor
• User defines the Explicit Cursor
• Involves specific operations to use the cursor.
• Has a definite name.
• Multiple rows are selected using Explicit
Cursor
Defining Explicit Cursor
Declare
CURSOR cur_xmp_cursor IS
(SELECT * from emplyees_tab);
Begin
----<other code>
Exception
---<exception handling>
END:
Cursor Name
Table Name
Cursor Operations
• Define the cursor.
• Open the Cursor.
• Fetch the cursor
• Close the cursor.
Operations Explained.
DECLARE
CURSOR curr_exp_examp IS
(SELECT * FROM employees_tab);
L_employee_row employees_tab%ROWTYPE;
BEGIN
OPEN curr_exp_examp ;
LOOP
FETCH curr_exp_examp INTO L_employee_row ;
< CODE TO PROCESS>
EXIT WHEN curr_exp_examp %NOTFOUND;
END LOOP;
CLOSE curr_exp_examp ;
END;
Example
• DECLARE
• CURSOR CUR_EXP_EXAMP IS(
• SELECT *
• FROM EMPLOYEES);
• L_EMP_ROW EMPLOYEES%ROWTYPE; ---- OR L_EMP_ROW CUR_EXP_EXAMP%ROWTYPE;
• BEGIN
• OPEN CUR_EXP_EXAMP;
• LOOP
• FETCH CUR_EXP_EXAMP
• INTO L_EMP_ROW;
• DBMS_OUTPUT.PUT_LINE('employee name->' || L_EMP_ROW.EMP_NAME);
• EXIT WHEN CUR_EXP_EXAMP%NOTFOUND;
• END LOOP;
• CLOSE CUR_EXP_EXAMP;
• END;

Más contenido relacionado

La actualidad más candente

Trigger and cursor program using sql
Trigger and cursor program using sqlTrigger and cursor program using sql
Trigger and cursor program using sql
Sushil Mishra
 

La actualidad más candente (20)

Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQL
 
Trigger and cursor program using sql
Trigger and cursor program using sqlTrigger and cursor program using sql
Trigger and cursor program using sql
 
Plsql
PlsqlPlsql
Plsql
 
Cursors
CursorsCursors
Cursors
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
Oracle Baisc Tutorial
Oracle Baisc TutorialOracle Baisc Tutorial
Oracle Baisc Tutorial
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
Pl sql guide
Pl sql guidePl sql guide
Pl sql guide
 
Plsql
PlsqlPlsql
Plsql
 
My sql cursors
My sql cursorsMy sql cursors
My sql cursors
 
SQL
SQLSQL
SQL
 
PL/SQL
PL/SQLPL/SQL
PL/SQL
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
Oracle - Program with PL/SQL - Lession 02
Oracle - Program with PL/SQL - Lession 02Oracle - Program with PL/SQL - Lession 02
Oracle - Program with PL/SQL - Lession 02
 
Triggers in plsql
Triggers in plsqlTriggers in plsql
Triggers in plsql
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 
Exception handling in plsql
Exception handling in plsqlException handling in plsql
Exception handling in plsql
 

Similar a Basic cursors in oracle

plsql tutorialhub....
plsql tutorialhub....plsql tutorialhub....
plsql tutorialhub....
Abhiram Vijay
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
Ashwin Kumar
 
Cursors in oracle
Cursors in oracleCursors in oracle
Cursors in oracle
Tamizhmuhil
 

Similar a Basic cursors in oracle (20)

4 cursors
4 cursors4 cursors
4 cursors
 
plsql tutorialhub....
plsql tutorialhub....plsql tutorialhub....
plsql tutorialhub....
 
SQL / PL
SQL / PLSQL / PL
SQL / PL
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
 
Oracle:Cursors
Oracle:CursorsOracle:Cursors
Oracle:Cursors
 
Oracle - Program with PL/SQL - Lession 06
Oracle - Program with PL/SQL - Lession 06Oracle - Program with PL/SQL - Lession 06
Oracle - Program with PL/SQL - Lession 06
 
Cursors
CursorsCursors
Cursors
 
Triggers n Cursors.ppt
Triggers n Cursors.pptTriggers n Cursors.ppt
Triggers n Cursors.ppt
 
Cursors in oracle
Cursors in oracleCursors in oracle
Cursors in oracle
 
Day 6.pptx
Day 6.pptxDay 6.pptx
Day 6.pptx
 
Cursors.ppt
Cursors.pptCursors.ppt
Cursors.ppt
 
PL_SQL - II.pptx
PL_SQL - II.pptxPL_SQL - II.pptx
PL_SQL - II.pptx
 
8. sql
8. sql8. sql
8. sql
 
Module07
Module07Module07
Module07
 
PL-SQL.pdf
PL-SQL.pdfPL-SQL.pdf
PL-SQL.pdf
 
Procedures andcursors
Procedures andcursorsProcedures andcursors
Procedures andcursors
 
Les21[1]Writing Explicit Cursors
Les21[1]Writing Explicit CursorsLes21[1]Writing Explicit Cursors
Les21[1]Writing Explicit Cursors
 
Lecture 3. MS SQL. Cursors.
Lecture 3. MS SQL. Cursors.Lecture 3. MS SQL. Cursors.
Lecture 3. MS SQL. Cursors.
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 

Basic cursors in oracle

  • 2. Definition • A cursor is a temporary work area created in the system memory when a SQL statement is executed. A cursor contains information on a select statement and the rows of data accessed by it. • Can hold information of more than one row. • One row at a time can be accessed.
  • 3. Cursor types • Implicit Cursor • Explicit Cursor
  • 4. Implicit Cursor • Oracle internally creates Implicit cursor • Whenever a DML operation like SELECT,UPDATE,INSERT happens on a row Oracle creates the cursor internally. • SELECT on a single row is a implicit cursor. • Status of the Implicit cursor can be found using SQL%. • Use cannot specify a name to Implicit Cursor.
  • 5. Implicit Cursor SQL%FOUND The return value is TRUE, if the DML statements like INSERT, DELETE and UPDATE affect at least one row and if SELECT ….INTO statement return at least one row. SQL%ROWCOUNT The return value is FALSE, if DML statements like INSERT, DELETE and UPDATE at least one row and if SELECT ….INTO statement return at least one row SQL%NOTFOUND Return the number of rows affected by the DML operations INSERT, DELETE, UPDATE, SELECT
  • 6. Explicit Cursor • User defines the Explicit Cursor • Involves specific operations to use the cursor. • Has a definite name. • Multiple rows are selected using Explicit Cursor
  • 7. Defining Explicit Cursor Declare CURSOR cur_xmp_cursor IS (SELECT * from emplyees_tab); Begin ----<other code> Exception ---<exception handling> END: Cursor Name Table Name
  • 8. Cursor Operations • Define the cursor. • Open the Cursor. • Fetch the cursor • Close the cursor.
  • 9. Operations Explained. DECLARE CURSOR curr_exp_examp IS (SELECT * FROM employees_tab); L_employee_row employees_tab%ROWTYPE; BEGIN OPEN curr_exp_examp ; LOOP FETCH curr_exp_examp INTO L_employee_row ; < CODE TO PROCESS> EXIT WHEN curr_exp_examp %NOTFOUND; END LOOP; CLOSE curr_exp_examp ; END;
  • 10. Example • DECLARE • CURSOR CUR_EXP_EXAMP IS( • SELECT * • FROM EMPLOYEES); • L_EMP_ROW EMPLOYEES%ROWTYPE; ---- OR L_EMP_ROW CUR_EXP_EXAMP%ROWTYPE; • BEGIN • OPEN CUR_EXP_EXAMP; • LOOP • FETCH CUR_EXP_EXAMP • INTO L_EMP_ROW; • DBMS_OUTPUT.PUT_LINE('employee name->' || L_EMP_ROW.EMP_NAME); • EXIT WHEN CUR_EXP_EXAMP%NOTFOUND; • END LOOP; • CLOSE CUR_EXP_EXAMP; • END;