SlideShare ist ein Scribd-Unternehmen logo
1 von 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;

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
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
 
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
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
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
 

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;