BDA Cursores
BDA Cursores
UNIVERSIDAD DE GUAYAQUIL
Carrera: SOftware
CURSORES
CONTENTS
1. INTRODUCTION of Cursors
2. Types of cursors
2.1 Implicit cursor
►Example
2.2 explicit cursor
►Example
CURSOR ?
• 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.
• This temporary work area is used to store the data retrieved from the database, and
perform intermediate operations before output is displayed to the client.
• A cursor can hold more than one row, but can process only one row at a time.
4
ORACLE
ORACLE ENGINE DATABASE
SQL ENGINE
QUERY
Data
Dictionary
PL/SQL
ENGINE Actual Data
CURSOR
-----Record pointer
OUTPUT Active Data Set
CLIENT
E101 abc Administration
… … … …
5
So, PL/SQL requires a special compatibility to retrieve and process more than one row
6
TYPES OF CURSORS
IMPLICIT EXPLICIT
CURSORS
8
Implicit cursors
● These are created by default when DML statements like,
INSERT, UPDATE, and DELETE statements are executed.
● The user is not aware of this happening & will not be able
to control or process the information.
Attribute Description
Name
%ISOPEN Returns TRUE if cursor is open, FALSE if cursor is closed.
SQL%ISOPEN always returns FALSE.
%FOUND Returns TRUE if successful fetch has been
executed, FALSE if no row was returned.
SQL%FOUND is used to access it.
EXAMPLE:
Write a pl/sql program to update the salary of customers by Rs 500. EXAMPLE:
Select * from customers;
+‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+
| ID | NAME | AGE | ADDRESS | SALARY |
+‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22| MP | 4500.00 |
+‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+
11
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET salary = salary + 500;
IF sql%notfound THEN
dbms_output.put_line('no customers selected');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ‘customers selected ');
END IF;
END;
12
Result:
6 customers selected
PL/SQL procedure successfully completed.
Explicit cursors
• Explicit cursors are programmer defined cursors
for gaining more control over the context area.
Attribute Description
Name
%ISOPEN Returns TRUE if cursor is open, FALSE if cursor is closed.
CursorName%ISOPEN is used to access it.
STEPS:
No
Yes
DECLARE OPEN FETCH CLOSE
• Create a • Identify the • Load the • Test for existing • Release the
named SQL active set current row into rows active set
area variables
• Return to
FETCH if rows
found
17
Cursor Declaration:
• Declared as a variable in the same way as standard
variables
• SQL included
Fetch
Pointer
Cursor
Cursor
Fetch a row from the cursor.
Pointer
Cursor
Close
Open the cursor.
Pointer
Cursor
Fetch a row from the cursor.
Pointer
Cursor
Pointer
Cursor
Close the cursor. After the FETCH loop exits, the cursor must
be closed by Close statement.
Cursor
Releases the used memory.
22
EXAMPLE:
DECLARE
c_id customers.id%type;
c_name customers.name%type;
c_addr customers.address%type;
CURSOR c_customers IS SELECT id, name, address FROM customers;
BEGIN
OPEN c_customers;
LOOP
FETCH c_customers into c_id, c_name, c_addr;
EXIT WHEN c_customers%notfound;
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);
END LOOP;
CLOSE c_customers;
END;
25
RESULT:
1 Ramesh Ahmedabad
2 Khilan Delhi
3 kaushik Kota
4 Chaitali Mumbai
5 Hardik Bhopal
6 Komal MP
Example 2:
The bank manager has decided to mark all those accounts as inactive (I) on
which there are no transactions performed in the last 365 days. Whenever any
such update takes place, a record for the same is maintained in the
INACTIVE_ACCT_MSTR table comprising of the account number, the
opening date and the type of account. Write a PL/SQL block to do the same.
Tables:
DECLARE
CURSOR Crsr_NoTrans IS
SELECT AcctNo, Status, OpenDt, Type FROM ACCT_MSTR
WHERE AcctNo IN (SELECT AcctNo FROM TRANS_MSTR
GROUP BY AcctNo HAVING MAX(SYSDATE-Dt)>365);
str_AcctNo ACCT_MSTR.AcctNo%type;
str_Status ACT_MSTR.Status%type;
dt_OpenDt ACCT_MSTR.OpenDt%type;
str_Type ACCT_MSTR.Type%type;
BEGIN
OPEN Crsr_NoTrans;
IF Crsr_NoTrans%ISOPEN THEN
LOOP
FETCH Crsr_NoTrans INTO str_AcctNo, str_Status, dt_OpenDt, str_type;
28
END IF;
END LOOP;
ELSE
dbms_output.put_line(‘Unable to open cursor’);
END IF;
CLOSE Crsr_NoTrans;
END;
29
EXAMPLE:
DECLARE
CURSOR c1
IS SELECT last_name, job_id FROM employee job_id LIKE '%CLERK%'
AND manager_id > 120
ORDER BY last_name;
BEGIN
FOR item IN c1
LOOP
DBMS_OUTPUT.PUT_LINE ('Name = ' || item.last_name || ', Job = ' ||
item.job_id);
END LOOP;
END;
31
RESULT:
Parameterized Cursors:
Parameterized cursor pass the parameters into a cursor and
use them in to query.
Syntax :
CURSOR cursor_name (parameter_list) IS
SELECT_statement;
For example:
CURSOR c2 (subject_id_in IN varchar) IS
SELECT course_number
FROM courses_tbl
WHERE subject_id = subject_id_in;
COMPARISION:
•Explicit cursor must be created when you are executing a SELECT statement that
returns more than one row.
•With explicit cursors, you have complete control over how to access information
in the database.
ADVANTAGES:
Disadvantages:
References:
2. www.wikipedia.com.
38