InterviewSolution
Saved Bookmarks
| 1. |
How to access the fields of a record in PL/SQL? |
|
Answer» One of the Collection types in PL/SQL is Index-By table. These are also called Associative Arrays. It gets created only in the PL/SQL block. The subscript TYPE for Associative Array is a string or integer. Associative Array is a set of key-value pair. The key here can be an Integer or String. The following is the syntax: TYPE type_name IS TABLE OF element_type [NOT NULL] INDEX BY subscript_type; name_of_table type_name;Here, index-by table is the name_of_table. The keys of the subscript_type. The associated VALUES will be of the element_type i.e. NUMBER, etc. The following is an example: DECLARE TYPE budget IS TABLE OF NUMBER INDEX BY VARCHAR2(15); dept_budget budget ; dept_name VARCHAR2(20); BEGIN dept_budget ('Operations') := 30000; dept_budget ('Finance') := 25000; dept_budget ('ACCOUNTS') := 45000; dept_name := dept_budget.FIRST; WHILE dept_name IS NOT null LOOP dbms_output.put_line ('Budget - ' || dept_name || ' Department = ' || TO_CHAR(dept_budget (dept_name ))); dept_name := dept_budget .NEXT(dept_name ); END LOOP; END; /The OUTPUT: Budget - Accounts Department = 45000 Budget - Finance Department = 25000 Budget - Operations Department = 30000 |
|