PL/SQL TABLES:
a) Objects of type TABLE are called PL/SQL tables.
b) They are modeled as database tables, but are not same.
c) PL/SQL tables use a Primary key to give array like access to Rows.
Things To Note:
--------------
i) It is similar to an Array.
ii) It shouls contains two components.
* A PRIMARY KEY of datatype BINARY_INTEGER, that Indexes the PL/SQL Table.
* A Column of a scalar or Record datatyoe which stores the PL/SQL TABLE elements.
iii) It can increase dynamically as it is unconstrained.
CREATING A PL/SQL TABLE:
=========================
a) There are two steps involved in creating a PL/SQL Table.
-> Declare a TABLE Datatype.
-> Declare a Variable of that Datatype.
Syntax:
TYPE TypeName IS TABLE OF
{ ColumnType | Variable%TYPE
| Table.Column%TYPE} [NOT NULL]
[INDEX BY BINARY_INTEGER];
Identifier TypeName;
Example:
TYPE Ename_Table_TYOE IS TABLE OF Emp.Ename%TYPE INDEX BY BINARY INTEGER;
EnameTable Ename_Table_Type;
PL/SQL TABLE STRUCTURE:
PRIMARY KEY COLUMN
1 SATISH
2 RAJ
3 KRISHNA
BINARY_INTEGER SCALAR
a) The number of rows in PL/SQL table can increase dynamically, Hence a PL/SQL table can grow as new rows are added.
b) PL/SQL tables can have one column and a PRIMARY KEY, neither of which can be named.
c) The column can belong to any scalar or record datatype, but the PRIMARY KEY must belong to Typr BINARY_INTEGER.
d) We cannot initialize a PL/SQL table in its declaration.
REFERENCING A PL/SQL TABLE:
Syntax:
PL/SQL_Table_Name(Primary_Key_Value);
a) Primary_Key_Value belongs to type BINARY_INTEGER.
b) The Primary Key value can be negative Indexing need not start with 1.
c) The Methods that make PL/SQL Tables easier to use are
* EXISTS(n) -> Returns TRUE if the nth element in a PL/SQL table exists.
* COUNT -> Returns the number of elements that PL/SQL TABLE currently contains.
* FIRST and
LAST -> Returns the FIRST and Last index numbers in PL/SQL Table. Returns NULL if the PL/SQL table is empty.
* Prior(n) -> Returns the Index number that preceeds index n in a PL/SQL Table.
* NEXT(n) -> Returns the Index number taht succeeds Index n in a PL/SQL Table.
* EXTEND(n.i) -> It increases the size of a PL/SQL Table.
i) It appends one NULL element to a PL/SQL Table.
ii) EXTEND(n) appends n NULL elements into a PL/SQL table.
iii) EXTEND(n.i) appends n copies of the ith element to a PL/SQL Table.
* TRIM -> It removes one element from the end of a PL/SQL Table.
* DELETE -> It removes all elements from a PL/SQL Table.
DELETE(n), DELET(m,n)
d) To Reference PL/SQL Table of Records we use
-> Table(index).Field
Example:
SQL > DECLARE
TYPE Dept_Table_Type IS TABLE OF Dept.DName%TYPE INDEX BY BINARY_INTEGER;
My_Dept_Table Dept_Table_Type;
V_Count NUMBER(2);
BEGIN
SELECT COUNT(*) INTO V_Count FROM DEPT;
FOR MyIndex IN 1...V_Count
LOOP
SELECT Dname INTO My_Dept_Table(MyIndex) FROM DEPT WHERE Deptno=MyIndex*10;
END LOOP;
FOR MyIndex IN 1....V_Count
LOOP
DBMS_OUTPUT_PUTLINE(My_Dept_Table(MyIndex));
ENDLOOP;
END;
SQL > DECLARE
TYPE Dept_Table_Type IS TABLE OF Dept%ROWTYPE INDEX BY BINARY_INTEGER;
My_Dept_Table Dept_Table_Type;
V_Count NUMBER(2);
BEGIN
SELECT COUNT(*) INTO V_Count FROM DEPT;
FOR MyIndex IN 1...V_Count
LOOP
SELECT *INTO My_Dept_Table(MyIndex) FROM DEPT WHERE Deptno=MyIndex*10;
END LOOP;
FOR MyIndex IN 1....V_Count
LOOP
DBMS_OUTPUT_PUTLINE('Department NUmber'||My_Dept_Table(MyIndex).DeptNO||'Department Name'||My_Dept_Table(MyIndex).DeptName ||'Department Location'||My_Dept_Table(MyIndex).LOC);
ENDLOOP;
END;
[8:18 PM
|
0
comments
]



0 comments
Post a Comment
Do comment to make this blog better