Cursors 100112215205 Phpapp01
Cursors 100112215205 Phpapp01
What is Cursors ?
SQL:
> SELECT * FROM employee;
Open emp_cur ;
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,....)
• CLOSE cursor_name;
Static Explicit Cursors