Sie sind auf Seite 1von 2

Collections

Collections are similar to arrays available in most 3GLs


A collection is an object that contains other objects of similar type
It is an ordered group of elements of the same type
Each element is identified by a unique subscript that determines its position in the
collection

Step 1 Creating the collection type.


CREATE TYPE Color_tab_t AS TABLE OF VARCHAR2(30);
Step 2 Type of a TABLE used as a data type of a column. This column is having nested
table.
CREATE TABLE cars(
name VARCHAR2(12),
colors Color_tab_t)
NESTED TABLE colors STORE AS color_model_colors_tab;
Colors is the column having Color_tab_t type. The colors column is having the nested
table.
Step3 Inserting records.
insert into cars
values ('Maruti', Color_tab_t('RED','GREEN','BLUE'));
insert into cars
values
('Indica',Color_tab_t('CYAN','YELLOW','MAGENTA','BLACK'));

Step 4 -To retrieve color names through a PL/SQL block create a variable of Color_tab_t
type. Then fetch the nested table column into that variable for that row. Then
through loop show each color with the help of the variable.
To see all the colors available to Maruti car -DECLARE
colorlist Color_tab_t;
BEGIN
SELECT colors INTO colorlist FROM cars
WHERE name = 'Maruti';
FOR i IN 1..colorlist.COUNT
LOOP
dbms_output.put_line( colorlist(i) );
END LOOP;
END;

Das könnte Ihnen auch gefallen