Composite Variable in PL/SQL
Composite Variable are also known as complex variable used to create a variable consisting of many other component . It is of Basic two type;
1. User define type:
Such variable are to be created by user in declare section of a program , it syntax is given as follow:
TYPE type_name IS
(var1 datatype, var2 datatype,.......);
NOTE: 1-row at a time , no constraints.
After a type has been created for using it and object of same type must be created in declare section which can be done as follow:
OBJECTNAME typename;
After object has been created , it can be used in begin section as objectname.varname;
2. System define :
Such variable are defined by database system which is again of two types.
a. %ColumnType
Such type can be used to create variable having data type from a column in the table. Even this variable has to be created in
declare section.
Syntax:
VARNAME tablename.collumnname % TYPE;
example:
Eid salesrep.empid%TYPE;
b. %RowType
Such type can be used to create a variable which can hold one complete row from a table. Such variable also must be created in declare section.
Syntax:
row_var_name table_name % ROWTYPE;
example:
e salerep % ROWTYPE;
QUESTION: Write a program to illustrate use of complex variable .
DECLARE
TYPE emprecord IS
(
eid salesrep.empid % TYPE,
ename salerep.name % TYPE
);
e emprecord;
erecord salesrep % ROWTYPE;
sales salerep.salesrep % TYPE;
BEGIN
SELECT sales INTO sales
FROM salesrep
WHERE empid=101;
DBMS_OUTPUT.PUT_LINE('SALES OF EMPLOYEE 101 IS'||sales);
SELECT empid, name INTO e
FROM salerep
WHERE empid=101;
DBMS_OUTPUT.PUT_LINE('id & name of employee 101 is'||e.id||e.ename);
SELECT * INTO erec
FROM selesrep
WHERE empid=101;
DBMS_OUTPUT.PUT_LINE('name,sales and quota of employee 101 is'||erec.name||erac.sales||erac.quota);
END;
/
Comments
Post a Comment