PL SQL Session3 Record Table Cursor
PL SQL Session3 Record Table Cursor
Examples :
V_Name := Concat(v_firstname, v_lastname);
V_nm_length := Length (V_name) ;
Using SQL Functions in PL-
SQL
Example 17 Sample17.SQL
Record
Objects of type RECORD are called records.
Begin
v_emp1.empno := &empno;
End;
Record
We can assign one Record variable of same type to another
Begin
v_emp1.empno := &empno;
v_emp2 := v_emp1 ;
End;
Example 19 Sample19.SQL
Table
PL/SQL provides a composite datatype named
TABLE.
-- Declaring Variable
vnum numTabtyp ; -- This will make vnum as an array of numbers
BEGIN
vnum(0) := 100;
vnum(-43) := 4500;
vnum(25) := 4300;
END;
v_str tcharTyp;
Note : In PLSQL Table i.e Array you can store any number of
values no restriction or the upper limit
Table - Functions
PL SQL provides with some functions that you can use with table
only.
Next (n) : Gets the index or position of element next to element at position
‘n’ in array.
Prior (n) : Gets the index or position of element Prior to element at position
‘n’ in array.
Delete (n): Deletes the element at position or Index ‘n’ in the array.
Example 21 Sample21.SQL
Example 22 Sample22.SQL
Cursors
When you execute a SQL statement from PL/SQL,
the Oracle RDBMS assigns a private work area for
that statement.
CURSOR employee_cur IS
SELECT * FROM employee;
Cursors
In PLSQL only way we can refer to a SQL query
that returns muliple rows is through cursors.
Imagine the query in the last slide returned
following rows.
Work Area
Employee_cur is
a pointer to this
work area and 101 Rajesh 5000 10
data and our 102 Suresh 4500 20
plsql program can
refer to this rows
one by one using
103 Nilesh 4000 10
Data
this cursor
104 Jitesh 3500 20 (Active
Set)
Cursors – Types
Implict Cursor : Managed by Oracle itself
internally
Syntax :
Cursor <Cursor_Name> IS
<Query> ;
Explicit Cursor
Example:
Cursor c_emp IS
Select * from emp where sal > 4500;
Syntax :
Open <Cursor_Name> ;
Explicit Cursor
Example:
Open c_emp;
Explicit Cursor
Step3 : Fetch the rows
Syntax
Loop
Fetch <Cursor_Name> INTO <Variables> ;
Exit when <Condition>
End Loop;
Explicit Cursor
Example:
Loop
Fetch c_emp INTO v_empno, v_ename, v_sal ;
Exit When c_emp%NotFound;
End Loop;
Example:
Loop
Fetch c_emp INTO v_emp ;
Exit When c_emp%NotFound;
End Loop;
Syntax :
Close <Cursor_Name> ;
Explicit Cursor
Example:
Close c_emp;
Cursors - Summary
In case of Implicit cursor the open , close fetch
everything is managed by oracle.
Attribute Explanation
%ISOPEN - Returns TRUE if the cursor is open, FALSE if the cursor is closed.
Cursor_Name%Attribute ;
Example:
c_emp%Found;
c_emp%NotFound;
c_emp%RowCount;
c_emp%IsOpen
Cursors attributes
In case of Implicit cursor we say:
SQL%Attribute ;
Example:
SQL%Found;
SQL%NotFound;
SQL%RowCount;
SQL%IsOpen
Example
Example 23 Sample23.SQL
Example 24 Sample24.SQL
Example 25 Sample25.SQL
Example 26 Sample26.SQL