-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarray_01.sql
51 lines (43 loc) · 1.87 KB
/
varray_01.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
SET SERVEROUTPUT ON;
DECLARE
/*
+ Array in other languages - VARRAY in PL/SQL: Collection of items of same datatype & has a maximum size.
+ When defining a VARRAY type, you must specify its maximum size. So fixed upper bound. Subscript is integer (i.e. index) starts from 1
+ VARRAY is always dense (consecutive subscript). You cannot delete an item in middle, but we can trim elements from end.
+ VARRAY is preferred when no. of elements known & accessed using sequence i.e. index.
*/
TYPE va_char_type IS
VARRAY(4) OF VARCHAR(15);
v1 va_char_type := va_char_type('East', 'West', 'North', 'South'); -- initialize varray in declaration using constructor
v2 va_char_type; -- Not initialized, hence this null
v3 va_char_type := va_char_type(); -- initialize empty varray
BEGIN
v2 := va_char_type('North-East', 'South-East'); -- initialize varray here using constructor
dbms_output.put_line('Iterating varray v1');
FOR i IN 1..v1.count LOOP
dbms_output.put_line('Index : '
|| i
|| ' and Value : '
|| v1(i));
END LOOP;
dbms_output.put_line('Iterating varray v2');
FOR i IN 1..v2.count LOOP
dbms_output.put_line('Index : '
|| i
|| ' and Value : '
|| v2(i));
END LOOP;
-- in case of v3, first lets extend (Assigning Null Values to Varray)
v3.extend(1);
v3(1) := 'Hello';
v3.extend(1);
v3(2) := 'Hello Again!';
dbms_output.put_line('Iterating varray v3');
FOR i IN 1..v3.count LOOP
dbms_output.put_line('Index : '
|| i
|| ' and Value : '
|| v3(i));
END LOOP;
END;
/