Examples of Oracle PL/SQL Cursors:
1)
Retrieving the records from the emp table and displaying them one by one using cursors:
CREATE OR REPLACE PROCEDURE my_proc IS
var_empno emp.empno%type;
var_ename emp.ename%type;
var_sal emp.sal%type;
//declaring a cursor//
CURSOR EMP_CURSOR IS
select empno, ename, sal from emp;
BEGIN
//opening a cursor//
open EMP_CURSOR;
LOOP
//fetching records from a cursor//
fetch EMP_CURSOR into var_empno, var_ename, var_sal;
//testing exit conditions//
EXIT when EMP_CURSOR%NOTFOUND;
IF (var_sal > 1000) then
DBMS_OUTPUT.put_line(var_empno || ' ' || var_ename || ' ' || var_sal);
ELSE
DBMS_OUTPUT.put_line(var_ename || ' sal is less then 1000');
END IF;
END LOOP;
//closing the cursor//
close EMP_CURSOR;
DBMS_OUTPUT.put_line('DONE');
END;
2)
Create a PL/SQL block to increase salary of employees in department 17.
a) The salary increase is 20% for the employees making less than $90,000 and 12% for the employees making $90,000 or more.
b) Use a cursor with a FOR UPDATE clause.
c) Update the salary with a WHERE CURRENT OF clause in a cursor FOR loop (cursor FOR loop problem).
PL/SQL procedure successfully completed.
3)
An example of an Implicit cursor
Oracle automatically associates an implicit cursor with the SELECT INTO statement and fetches
the values for the variables var_firstname and var_lastname. After the SELECT INTO statement completes, Oracle closes the implicit cursor.
Examples of Oracle PL/SQL Cursors
·
Oracle SQL
Archives
-
▼
2010
(49)
-
▼
February
(49)
- Null Values in SQL Group By
- Examples of SQL Having & SQL Group By
- SQL Inline Views
- SQL Join View
- Modifying SQL Views (Insert, Update and Delete on ...
- SQL Scalar subquery
- SQL Correlated Subquery
- SQL Inline View Subquery
- Examples of Oracle PL/SQL Cursors
- Oracle PL/SQL WHERE CURRENT OF & FOR UPDATE
- Oracle PL/SQL REF Cursors
- Oracle PL/SQL Cursors With Parameters
- Oracle PL/SQL Cursor For Loops
- Oracle PL/SQL Explicit Cursors
- Oracle PL/SQL Implicit Cursors
- PL/SQL Programmer Defined Records
- PL/SQL Cursor Based Records
- PL/SQL Table Based Records
- Oracle PL/SQL Nested Tables
- Oracle PL/SQL Varrays
- Oracle PL/SQL Tables
- Oracle PL/SQL Records
- SQL PL/SQL Interview Questions
- PL/SQL Triggers
- Oracle PL/SQL Cursors
- PL/SQL Collections
- PL/SQL Introduction
- Join Using Multiple Tables (more than 2)
- SQL Equi-join
- SQL Cross Join
- Example of SQL Self Join
- SQL Self Join
- Example of SQL Inner Join
- SQL Inner Join/ SQL Natural Join:
- Example of SQL Outer Join
- SQL Outer Join
- Example of Simple SQL Join
- SQL Interview Questions
- SQL Views
- SQL Subqueries
- SQL Having
- SQL Group By Examples
- SQL Group By
- SQL Joins
- SQL Delete
- SQL Update
- SQL Insert
- SQL Syntax
- SQL Introduction
-
▼
February
(49)