PLSQL Cursors
-
Upload
spinnaresh -
Category
Technology
-
view
6.076 -
download
4
description
Transcript of PLSQL Cursors
![Page 1: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/1.jpg)
PL/SQL Cursors
By Praveen & Naresh
![Page 2: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/2.jpg)
What is Cursors ?
SQL: > SELECT * FROM employee;
Oracle RDBMS : Assigns a private work area for that stmt.
![Page 3: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/3.jpg)
contd..
PL/SQL cursor is a mechanism by which
“ you can NAME that work area
and
manipulate the information within it”
![Page 4: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/4.jpg)
Declare/Open/Fetch/Close
CURSOR emp_cur IS SELECT * FROM employee ;
Open emp_cur ;
Fetch emp_cur INTO emp_rec;
CLOSE emp_cur ;
![Page 5: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/5.jpg)
Types of cursors
Two Types:
1) Implicit
2) Explicit
![Page 6: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/6.jpg)
Cursor Attributes
Attribute:
%FOUND
%ISOPEN
%NOTFOUND
%ROWCOUNT
![Page 7: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/7.jpg)
Implicit Cursors
• DML Statements• INSERT, UPDATE, DELETE• SELECT statement with INTO clause
![Page 8: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/8.jpg)
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;
/
![Page 9: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/9.jpg)
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;
/
![Page 10: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/10.jpg)
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;
/
![Page 11: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/11.jpg)
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.
![Page 12: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/12.jpg)
Explicit Cursors
• Define inside declaration block• Static or dynamic SELECT stmt.• Open/Fetch/Close
![Page 13: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/13.jpg)
Syntax
• OPEN cursor_name [ (param1 , param2 ,....) ]
• FETCH cursor_name INTO (variable1,variable2,....)
• FETCH cursor_name INTO record_variable;
• CLOSE cursor_name;
![Page 14: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/14.jpg)
Static Explicit Cursors
• SQL SELECT – behavior doesn't change
![Page 15: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/15.jpg)
Static CursorDECLARE
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;
/
![Page 16: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/16.jpg)
Eg- Cursor For LoopDECLARE
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;
/
![Page 17: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/17.jpg)
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;
/
![Page 18: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/18.jpg)
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
![Page 19: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/19.jpg)
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;
/
![Page 20: PLSQL Cursors](https://reader033.fdocuments.net/reader033/viewer/2022061202/547cb4c2b4af9fbb378b46a0/html5/thumbnails/20.jpg)
Thanks