0% found this document useful (0 votes)
33 views19 pages

Cursors 100112215205 Phpapp01

The document discusses PL/SQL cursors. It defines a cursor as a mechanism to name and manipulate a work area that is assigned by the Oracle RDBMS to process a SQL statement. There are two types of cursors: implicit and explicit. Implicit cursors are associated with DML statements like INSERT, UPDATE, DELETE, while explicit cursors must be declared, opened, fetched from, and closed. Explicit cursors allow looping through result sets and accessing attributes like %FOUND, %ISOPEN, %NOTFOUND, and %ROWCOUNT.

Uploaded by

Dhruv Arora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views19 pages

Cursors 100112215205 Phpapp01

The document discusses PL/SQL cursors. It defines a cursor as a mechanism to name and manipulate a work area that is assigned by the Oracle RDBMS to process a SQL statement. There are two types of cursors: implicit and explicit. Implicit cursors are associated with DML statements like INSERT, UPDATE, DELETE, while explicit cursors must be declared, opened, fetched from, and closed. Explicit cursors allow looping through result sets and accessing attributes like %FOUND, %ISOPEN, %NOTFOUND, and %ROWCOUNT.

Uploaded by

Dhruv Arora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

PL/SQL Cursors

What is Cursors ?

SQL:
> SELECT * FROM employee;

Oracle RDBMS : Assigns a private work area for that


stmt.
contd..
PL/SQL cursor is a mechanism by which

“ you can NAME that work area


and
manipulate the information within it”
Declare/Open/Fetch/Close

CURSOR emp_cur IS SELECT * FROM employee ;

Open emp_cur ;

Fetch emp_cur INTO emp_rec;

CLOSE emp_cur ;
Types of cursors

Two Types:
1) Implicit
2) Explicit
Cursor Attributes
Attribute:

%FOUND
%ISOPEN
%NOTFOUND
%ROWCOUNT
Implicit Cursors
• DML Statements
• INSERT, UPDATE, DELETE
• SELECT statement with INTO clause
Single Row Implicit Cursors
DECLARE
e_id employee.empid%TYPE;
e_name employee.empname%TYPE;
BEGIN
SELECT empid, empname INTO e_id, e_name
FROM employee where empid=23;

dbms_output.put_line( SQL%ROWCOUNT );
END;
/
Multiple Row Implicit Cursors(1)
BEGIN
UPDATE employee SET emp_sal= emp_sal + 5000
where emp_deptid=1;

IF SQL%FOUND THEN
dbms_output.put_line('Updated:'|| SQL%ROWCOUNT );
ELSE
dbms_output.put_line( 'Nothing Updated' );
END;
/
Multiple Row Implicit Cursors(2)

BEGIN
FOR e_cursor IN ( SELECT e_name FROM employee ) LOOP
dbms_output.put_line( e_cursor.e_name );
END LOOP;
END;
/
Cursor FOR Loop
Scope : Inside FOR Loop

Cursor Index :
- a pointer to Query work area.
- Query work area is a memory region ( context
area )

Note:
SQL%ROWCOUNT attribute returns NULL.
Explicit Cursors
• Define inside declaration block
• Static or dynamic SELECT stmt.
• Open/Fetch/Close
Syntax
• OPEN cursor_name [ (param1 , param2 ,....) ]

• FETCH cursor_name
INTO (variable1,variable2,....)

• FETCH cursor_name INTO record_variable;

• CLOSE cursor_name;
Static Explicit Cursors

• SQL SELECT – behavior doesn't change


Static Cursor
DECLARE
eid employee.empid%TYPE;
ename employee.empname%TYPE;
CURSOR cur IS
SELECT empid, empname FROM employee ;
BEGIN
OPEN cur ;
LOOP
FETCH cur INTO eid , ename;
EXIT WHEN cur%NOTFOUND;
dbms_output.put_line( 'NAME' || cur.ename );
END LOOP;
CLOSE cur ;
END;
/
Eg- Cursor For Loop
DECLARE
CURSOR C IS
SELECT empid AS eid , empname AS ename FROM employee ;
BEGIN
FOR i IN C LOOP
dbms_output.put_line( 'NAME' || i.ename );
END LOOP;
END;
/
Static Cursor( Record)
DECLARE
TYPE emp_record IS RECORD ( eid NUMBER, ename VARCHAR2(30) );
employee EMP_RECORD;

CURSOR cur IS SELECT empid, empname FROM employee ;


BEGIN
OPEN cur ;
LOOP
FETCH cur INTO employee;
EXIT WHEN cur%NOTFOUND;
dbms_output.put_line( 'NAME' || employee.ename );
END LOOP;
CLOSE cur ;
END;
/
Static Cursor( Record+FOR Loop)
DECLARE
TYPE emp_record IS RECORD ( eid NUMBER, ename VARCHAR2(30) );
explicit_employee EMP_RECORD;

CURSOR cur IS SELECT empid, empname FROM employee ;


BEGIN
FOR i IN cur LOOP
explicit_employee := i;
dbms_output.put_line( 'NAME' || explicit_employee.ename );
END LOOP;
END;
/
Note: No Data Found , No error is raised
Static Cursor( No rows)
DECLARE
TYPE emp_record IS RECORD ( eid NUMBER, ename VARCHAR2(30) );
employee EMP_RECORD;
CURSOR cur IS SELECT empid, empname FROM employee where empid = -1 ;
BEGIN
OPEN cur ;
LOOP
FETCH cur INTO employee;
IF cur%NOTFOUND THEN
IF cur%ROWCOUNT = 0 THEN
dbms_output.put_line( 'No Data Found' );
END IF;
EXIT;
ELSE
dbms_output.put_line( 'NAME' || employee.ename );
END IF;
END LOOP;
CLOSE cur ;
END;
/

You might also like