Details
-
Task
-
Status: Open (View Workflow)
-
Major
-
Resolution: Unresolved
-
None
-
None
Description
This task is for using Oracle style VARRAYs inside stored routines.
A VARRAY is single-dimensional collections of elements with the same data type.
A VARRAY always has a fixed number of elements and never has gaps between the elements (not sparse).
Declare a VARRAY type
TYPE array_type_name IS VARRAY(max_elements) OF element_type [NOT NULL]; |
- array_type_name is the type of the VARRAY.
- max_elements is the maximum number of elements allowed in the VARRAY.
- NOT NULL specifies that the element of the VARRAY of that type cannot have NULL elements. Note that a VARRAY variable can be null, or uninitialized.
- element_type is the type of elements of the VARRAY type’s variable.
Example:
DECLARE
|
TYPE array_type_name IS VARRAY(10) OF INT; |
BEGIN
|
NULL; |
END; |
/
|
Declare a global VARRAY type
To create a VARRAY type which is accessible globally in the database, not just in PL/SQL code, one uses the following syntax:
CREATE [OR REPLACE ] TYPE type_name AS | IS |
VARRAY(max_elements) OF element_type [NOT NULL]; |
Global VARRAY types is out of scope of this task and will be implemented separately.
Declare and initialize VARRAY variables
Once a VARRAY type is created, a VARRAY instance of that type can be declared. The syntax is:
varray_name type_name [:= type_name(...)];
|
- The varray_name is the name of the VARRAY.
- The type_name is the VARRAY type.
- The type_name(...) is the constructor of the VARRAY type, which accepts a comma-separated list of elements as arguments. It has the same name as the VARRAY type.
Example:
DECLARE
|
TYPE array_type_name IS VARRAY(10) OF INT; |
array_var array_type_name;
|
BEGIN
|
NULL; |
END; |
/
|
Before using a VARRAY variable, it must be initialized. Otherwise, an error is raised:
DECLARE
|
TYPE array_type_name IS VARRAY(10) OF INT; |
array_var array_type_name;
|
BEGIN
|
IF array_var(1) = 1 THEN |
NULL; |
END IF; |
END; |
/
|
ORA-06531: Reference to uninitialized collection ORA-06512: at line 5
|
The following syntax initialized a VARRAY variable to an empty collection (zero elements):
DECLARE
|
TYPE array_type_name IS VARRAY(10) OF INT; |
array_var1 array_type_name := array_type_name(); -- declare initializing |
array_var2 array_type_name;
|
BEGIN
|
array_var2 := array_type_name(); -- assignment initializing |
END; |
/
|
The following syntax specifies elements while initializing a VARRAY:
DECLARE
|
TYPE array_type_name IS VARRAY(10) OF INT; |
array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing |
array_var2 array_type_name;
|
BEGIN
|
array_var2 := array_type_name(1,2,3); -- assignment initializing |
END; |
/
|
A VARRAY can be assigneed to another VARRAY (all members are copied):
DECLARE
|
TYPE array_type_name IS VARRAY(10) OF INT; |
array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing |
array_var2 array_type_name;
|
BEGIN
|
array_var2 := array_var1;
|
END; |
/
|
Accessing VARRAY elements
To access an array element use the following syntax:
varray_name(n);
|
where n is the index of the element position between 1 and the maximum number of elements defined in the VARRAY type.
Example:
DECLARE
|
TYPE array_type_name IS VARRAY(10) OF INT; |
array_var array_type_name := array_type_name(1,2,3);
|
BEGIN
|
array_var(1):= array_var(1)*10;
|
DBMS_OUTPUT.PUT_LINE(array_var(1));
|
END; |
/
|
10
|
If the index is not in the range (1, initialized element count), the SUBSCRIPT_BEYOND_COUNT error is raised.
DECLARE
|
TYPE array_type_name IS VARRAY(10) OF INT; |
array_var array_type_name := array_type_name(1,2,3);
|
BEGIN
|
DBMS_OUTPUT.PUT_LINE(array_var(4));
|
END; |
/
|
ORA-06533: Subscript beyond count ORA-06512: at line 5
|
If the index is greater than the number of elements in the VARRAY type declaration, the SUBSCRIPT_BEYOND_COUNT error is also raised, but with a different error text:
DECLARE
|
TYPE array_type_name IS VARRAY(10) OF INT; |
array_var array_type_name := array_type_name(1,2,3);
|
BEGIN
|
DBMS_OUTPUT.PUT_LINE(array_var(11));
|
END; |
/
|
ORA-06532: Subscript outside of limit ORA-06512: at line 5
|
Adding elements
The method EXTEND adds elements to the end of a VARRAY.
- EXTEND appends one NULL element to the VARRAY
- EXTEND(n) appends n NULL elements to the VARRAY
- EXTEND(n,i) appends n copies of the i-th element to the collection.
Example:
DECLARE
|
TYPE array_type_name IS VARRAY(10) OF INT; |
array_var array_type_name := array_type_name(10);
|
BEGIN
|
array_var.EXTEND;
|
array_var(2):= 20;
|
array_var.EXTEND(2);
|
array_var(3):= 30;
|
array_var(4):= 40;
|
array_var.EXTEND(2,1);
|
FOR i IN 1..6 |
LOOP
|
DBMS_OUTPUT.PUT_LINE(array_var(i));
|
END LOOP; |
END; |
/
|
10
|
20
|
30
|
40
|
10
|
10
|
Deleting elements
To delete all elements of a VARRAY, you use the DELETE method:
array_var.DELETE; |
To remove one element from the end of a VARRAY, use the TRIM() method:
array_var.TRIM()
|
To remove n elements from the end of a VARRAY, use the TRIM method:
array_var.TRIM(n)
|
TODO: Describe other methods:
- EXISTS
- FIRST
- LAST
- COUNT
- LIMIT
Attachments
Issue Links
- is blocked by
-
MDEV-32380 Array data type for stored routnes
-
- Stalled
-
- relates to
-
MDEV-10143 User defined types
-
- Open
-
-
MDEV-34319 Associative arrays: DECLARE TYPE .. TABLE OF .. INDEX BY in stored routines
-
- Needs Feedback
-
-
MDEV-35980 Oracle: Nested tables for scalar elements
-
- Open
-
- links to
Activity
Field | Original Value | New Value |
---|---|---|
Link | This issue is blocked by MDEV-32380 [ MDEV-32380 ] |
Description |
This task is for using VARRAYs inside stored routines.
A VARRAY is single-dimensional collections of elements with the same data type. A VARRAY always has a fixed number of elements and never has gaps between the elements (not sparse). h2. Declare a VARRAY type {code:sql} TYPE array_type_name IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} - array_type_name is the type of the VARRAY. - max_elements is the maximum number of elements allowed in the VARRAY. - NOT NULL specifies that the element of the VARRAY of that type cannot have NULL elements. Note that a VARRAY variable can be null, or uninitialized. - element_type is the type of elements of the VARRAY type’s variable. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; BEGIN NULL; END; / {code} h2. Declare a global VARRAY type To create a VARRAY type which is accessible globally in the database, not just in PL/SQL code, one uses the following syntax: {code:sql} CREATE [OR REPLACE ] TYPE type_name AS | IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} Global VARRAY types is out of scope of this task and will be implemented separately. h2. Declare and initialize VARRAY variables Once a VARRAY type is created, a VARRAY instance of that type can be declared. The syntax is: {code:sql} varray_name type_name [:= type_name(...)]; {code} - The varray_name is the name of the VARRAY. - The type_name is the VARRAY type. - The type_name(...) is the constructor of the VARRAY type, which accepts a comma-separated list of elements as arguments. It has the same name as the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN NULL; END; / {code} Before using a VARRAY variable, it must be initialized. Otherwise, an error is raised: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN IF array_var(1) = 1 THEN NULL; END IF; END; / {code} {noformat} ORA-06531: Reference to uninitialized collection ORA-06512: at line 5 {noformat} |
This task is for using VARRAYs inside stored routines.
A VARRAY is single-dimensional collections of elements with the same data type. A VARRAY always has a fixed number of elements and never has gaps between the elements (not sparse). h2. Declare a VARRAY type {code:sql} TYPE array_type_name IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} - array_type_name is the type of the VARRAY. - max_elements is the maximum number of elements allowed in the VARRAY. - NOT NULL specifies that the element of the VARRAY of that type cannot have NULL elements. Note that a VARRAY variable can be null, or uninitialized. - element_type is the type of elements of the VARRAY type’s variable. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; BEGIN NULL; END; / {code} h2. Declare a global VARRAY type To create a VARRAY type which is accessible globally in the database, not just in PL/SQL code, one uses the following syntax: {code:sql} CREATE [OR REPLACE ] TYPE type_name AS | IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} Global VARRAY types is out of scope of this task and will be implemented separately. h2. Declare and initialize VARRAY variables Once a VARRAY type is created, a VARRAY instance of that type can be declared. The syntax is: {code:sql} varray_name type_name [:= type_name(...)]; {code} - The varray_name is the name of the VARRAY. - The type_name is the VARRAY type. - The type_name(...) is the constructor of the VARRAY type, which accepts a comma-separated list of elements as arguments. It has the same name as the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN NULL; END; / {code} Before using a VARRAY variable, it must be initialized. Otherwise, an error is raised: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN IF array_var(1) = 1 THEN NULL; END IF; END; / {code} {noformat} ORA-06531: Reference to uninitialized collection ORA-06512: at line 5 {noformat} Description to be continued. |
Description |
This task is for using VARRAYs inside stored routines.
A VARRAY is single-dimensional collections of elements with the same data type. A VARRAY always has a fixed number of elements and never has gaps between the elements (not sparse). h2. Declare a VARRAY type {code:sql} TYPE array_type_name IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} - array_type_name is the type of the VARRAY. - max_elements is the maximum number of elements allowed in the VARRAY. - NOT NULL specifies that the element of the VARRAY of that type cannot have NULL elements. Note that a VARRAY variable can be null, or uninitialized. - element_type is the type of elements of the VARRAY type’s variable. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; BEGIN NULL; END; / {code} h2. Declare a global VARRAY type To create a VARRAY type which is accessible globally in the database, not just in PL/SQL code, one uses the following syntax: {code:sql} CREATE [OR REPLACE ] TYPE type_name AS | IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} Global VARRAY types is out of scope of this task and will be implemented separately. h2. Declare and initialize VARRAY variables Once a VARRAY type is created, a VARRAY instance of that type can be declared. The syntax is: {code:sql} varray_name type_name [:= type_name(...)]; {code} - The varray_name is the name of the VARRAY. - The type_name is the VARRAY type. - The type_name(...) is the constructor of the VARRAY type, which accepts a comma-separated list of elements as arguments. It has the same name as the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN NULL; END; / {code} Before using a VARRAY variable, it must be initialized. Otherwise, an error is raised: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN IF array_var(1) = 1 THEN NULL; END IF; END; / {code} {noformat} ORA-06531: Reference to uninitialized collection ORA-06512: at line 5 {noformat} Description to be continued. |
This task is for using Oracle style VARRAYs inside stored routines.
A VARRAY is single-dimensional collections of elements with the same data type. A VARRAY always has a fixed number of elements and never has gaps between the elements (not sparse). h2. Declare a VARRAY type {code:sql} TYPE array_type_name IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} - array_type_name is the type of the VARRAY. - max_elements is the maximum number of elements allowed in the VARRAY. - NOT NULL specifies that the element of the VARRAY of that type cannot have NULL elements. Note that a VARRAY variable can be null, or uninitialized. - element_type is the type of elements of the VARRAY type’s variable. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; BEGIN NULL; END; / {code} h2. Declare a global VARRAY type To create a VARRAY type which is accessible globally in the database, not just in PL/SQL code, one uses the following syntax: {code:sql} CREATE [OR REPLACE ] TYPE type_name AS | IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} Global VARRAY types is out of scope of this task and will be implemented separately. h2. Declare and initialize VARRAY variables Once a VARRAY type is created, a VARRAY instance of that type can be declared. The syntax is: {code:sql} varray_name type_name [:= type_name(...)]; {code} - The varray_name is the name of the VARRAY. - The type_name is the VARRAY type. - The type_name(...) is the constructor of the VARRAY type, which accepts a comma-separated list of elements as arguments. It has the same name as the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN NULL; END; / {code} Before using a VARRAY variable, it must be initialized. Otherwise, an error is raised: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN IF array_var(1) = 1 THEN NULL; END IF; END; / {code} {noformat} ORA-06531: Reference to uninitialized collection ORA-06512: at line 5 {noformat} Description to be continued. |
Description |
This task is for using Oracle style VARRAYs inside stored routines.
A VARRAY is single-dimensional collections of elements with the same data type. A VARRAY always has a fixed number of elements and never has gaps between the elements (not sparse). h2. Declare a VARRAY type {code:sql} TYPE array_type_name IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} - array_type_name is the type of the VARRAY. - max_elements is the maximum number of elements allowed in the VARRAY. - NOT NULL specifies that the element of the VARRAY of that type cannot have NULL elements. Note that a VARRAY variable can be null, or uninitialized. - element_type is the type of elements of the VARRAY type’s variable. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; BEGIN NULL; END; / {code} h2. Declare a global VARRAY type To create a VARRAY type which is accessible globally in the database, not just in PL/SQL code, one uses the following syntax: {code:sql} CREATE [OR REPLACE ] TYPE type_name AS | IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} Global VARRAY types is out of scope of this task and will be implemented separately. h2. Declare and initialize VARRAY variables Once a VARRAY type is created, a VARRAY instance of that type can be declared. The syntax is: {code:sql} varray_name type_name [:= type_name(...)]; {code} - The varray_name is the name of the VARRAY. - The type_name is the VARRAY type. - The type_name(...) is the constructor of the VARRAY type, which accepts a comma-separated list of elements as arguments. It has the same name as the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN NULL; END; / {code} Before using a VARRAY variable, it must be initialized. Otherwise, an error is raised: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN IF array_var(1) = 1 THEN NULL; END IF; END; / {code} {noformat} ORA-06531: Reference to uninitialized collection ORA-06512: at line 5 {noformat} Description to be continued. |
This task is for using Oracle style VARRAYs inside stored routines.
A VARRAY is single-dimensional collections of elements with the same data type. A VARRAY always has a fixed number of elements and never has gaps between the elements (not sparse). h2. Declare a VARRAY type {code:sql} TYPE array_type_name IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} - array_type_name is the type of the VARRAY. - max_elements is the maximum number of elements allowed in the VARRAY. - NOT NULL specifies that the element of the VARRAY of that type cannot have NULL elements. Note that a VARRAY variable can be null, or uninitialized. - element_type is the type of elements of the VARRAY type’s variable. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; BEGIN NULL; END; / {code} h2. Declare a global VARRAY type To create a VARRAY type which is accessible globally in the database, not just in PL/SQL code, one uses the following syntax: {code:sql} CREATE [OR REPLACE ] TYPE type_name AS | IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} Global VARRAY types is out of scope of this task and will be implemented separately. h2. Declare and initialize VARRAY variables Once a VARRAY type is created, a VARRAY instance of that type can be declared. The syntax is: {code:sql} varray_name type_name [:= type_name(...)]; {code} - The varray_name is the name of the VARRAY. - The type_name is the VARRAY type. - The type_name(...) is the constructor of the VARRAY type, which accepts a comma-separated list of elements as arguments. It has the same name as the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN NULL; END; / {code} Before using a VARRAY variable, it must be initialized. Otherwise, an error is raised: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN IF array_var(1) = 1 THEN NULL; END IF; END; / {code} {noformat} ORA-06531: Reference to uninitialized collection ORA-06512: at line 5 {noformat} The following syntax initialized a VARRAY variable to an empty collection (zero elements): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(); -- assignment initializing END; / {code} The following syntax specifies elements while initializing a VARRAY: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(1,2,3); -- assignment initializing END; / {code} A VARRAY can be assigneed to another VARRAY (all members are copied): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_var1; END; / {code} h2. Accessing VARRAY elements To access an array element use the following syntax: {code:sql} varray_name(n); {code} where {{n}} is the index of the element position between 1 and the maximum number of elements defined in the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN array_var(1):= array_var(1)*10; DBMS_OUTPUT.PUT_LINE(array_var(1)); END; / {code} {noformat} 10 {noformat} If the index is not in the range (1, initialized element count), the SUBSCRIPT_BEYOND_COUNT error is raised. {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(4)); END; / {code} {noformat} ORA-06533: Subscript beyond count ORA-06512: at line 5 {noformat} If the index is greater than the number of elements in the VARRAY type declaration, the SUBSCRIPT_BEYOND_COUNT error is also raised, but with a different error text: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(11)); END; / {code} {noformat} ORA-06532: Subscript outside of limit ORA-06512: at line 5 {noformat} h2. Adding elements The method EXTEND adds elements to the end of a VARRAY. - EXTEND appends one NULL element to the VARRAY - EXTEND(n) appends n NULL elements to the VARRAY - EXTEND(n,i) appends n copies of the i-th element to the collection. Example: DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(10); BEGIN array_var.EXTEND; array_var(2):= 20; array_var.EXTEND(2); array_var(3):= 30; array_var(4):= 40; array_var.EXTEND(2,1); FOR i IN 1..6 LOOP DBMS_OUTPUT.PUT_LINE(array_var(i)); END LOOP; END; / {noformat} 10 20 30 40 10 10 {noformat} h2. Deleting elements To delete all elements of a VARRAY, you use the DELETE method: {code:sql} array_var.DELETE; {code} To remove one element from the end of a VARRAY, use the TRIM() method: {code:sql} array_var.TRIM() {code} To remove n elements from the end of a VARRAY, use the TRIM(n) method: {code:sql} array_var.TRIM(n) {code} h2. TODO: Describe other methods: - EXISTS - FIRST - LAST - COUNT - LIMIT |
Description |
This task is for using Oracle style VARRAYs inside stored routines.
A VARRAY is single-dimensional collections of elements with the same data type. A VARRAY always has a fixed number of elements and never has gaps between the elements (not sparse). h2. Declare a VARRAY type {code:sql} TYPE array_type_name IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} - array_type_name is the type of the VARRAY. - max_elements is the maximum number of elements allowed in the VARRAY. - NOT NULL specifies that the element of the VARRAY of that type cannot have NULL elements. Note that a VARRAY variable can be null, or uninitialized. - element_type is the type of elements of the VARRAY type’s variable. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; BEGIN NULL; END; / {code} h2. Declare a global VARRAY type To create a VARRAY type which is accessible globally in the database, not just in PL/SQL code, one uses the following syntax: {code:sql} CREATE [OR REPLACE ] TYPE type_name AS | IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} Global VARRAY types is out of scope of this task and will be implemented separately. h2. Declare and initialize VARRAY variables Once a VARRAY type is created, a VARRAY instance of that type can be declared. The syntax is: {code:sql} varray_name type_name [:= type_name(...)]; {code} - The varray_name is the name of the VARRAY. - The type_name is the VARRAY type. - The type_name(...) is the constructor of the VARRAY type, which accepts a comma-separated list of elements as arguments. It has the same name as the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN NULL; END; / {code} Before using a VARRAY variable, it must be initialized. Otherwise, an error is raised: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN IF array_var(1) = 1 THEN NULL; END IF; END; / {code} {noformat} ORA-06531: Reference to uninitialized collection ORA-06512: at line 5 {noformat} The following syntax initialized a VARRAY variable to an empty collection (zero elements): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(); -- assignment initializing END; / {code} The following syntax specifies elements while initializing a VARRAY: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(1,2,3); -- assignment initializing END; / {code} A VARRAY can be assigneed to another VARRAY (all members are copied): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_var1; END; / {code} h2. Accessing VARRAY elements To access an array element use the following syntax: {code:sql} varray_name(n); {code} where {{n}} is the index of the element position between 1 and the maximum number of elements defined in the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN array_var(1):= array_var(1)*10; DBMS_OUTPUT.PUT_LINE(array_var(1)); END; / {code} {noformat} 10 {noformat} If the index is not in the range (1, initialized element count), the SUBSCRIPT_BEYOND_COUNT error is raised. {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(4)); END; / {code} {noformat} ORA-06533: Subscript beyond count ORA-06512: at line 5 {noformat} If the index is greater than the number of elements in the VARRAY type declaration, the SUBSCRIPT_BEYOND_COUNT error is also raised, but with a different error text: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(11)); END; / {code} {noformat} ORA-06532: Subscript outside of limit ORA-06512: at line 5 {noformat} h2. Adding elements The method EXTEND adds elements to the end of a VARRAY. - EXTEND appends one NULL element to the VARRAY - EXTEND(n) appends n NULL elements to the VARRAY - EXTEND(n,i) appends n copies of the i-th element to the collection. Example: DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(10); BEGIN array_var.EXTEND; array_var(2):= 20; array_var.EXTEND(2); array_var(3):= 30; array_var(4):= 40; array_var.EXTEND(2,1); FOR i IN 1..6 LOOP DBMS_OUTPUT.PUT_LINE(array_var(i)); END LOOP; END; / {noformat} 10 20 30 40 10 10 {noformat} h2. Deleting elements To delete all elements of a VARRAY, you use the DELETE method: {code:sql} array_var.DELETE; {code} To remove one element from the end of a VARRAY, use the TRIM() method: {code:sql} array_var.TRIM() {code} To remove n elements from the end of a VARRAY, use the TRIM(n) method: {code:sql} array_var.TRIM(n) {code} h2. TODO: Describe other methods: - EXISTS - FIRST - LAST - COUNT - LIMIT |
This task is for using Oracle style VARRAYs inside stored routines.
A VARRAY is single-dimensional collections of elements with the same data type. A VARRAY always has a fixed number of elements and never has gaps between the elements (not sparse). h2. Declare a VARRAY type {code:sql} TYPE array_type_name IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} - array_type_name is the type of the VARRAY. - max_elements is the maximum number of elements allowed in the VARRAY. - NOT NULL specifies that the element of the VARRAY of that type cannot have NULL elements. Note that a VARRAY variable can be null, or uninitialized. - element_type is the type of elements of the VARRAY type’s variable. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; BEGIN NULL; END; / {code} h2. Declare a global VARRAY type To create a VARRAY type which is accessible globally in the database, not just in PL/SQL code, one uses the following syntax: {code:sql} CREATE [OR REPLACE ] TYPE type_name AS | IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} Global VARRAY types is out of scope of this task and will be implemented separately. h2. Declare and initialize VARRAY variables Once a VARRAY type is created, a VARRAY instance of that type can be declared. The syntax is: {code:sql} varray_name type_name [:= type_name(...)]; {code} - The varray_name is the name of the VARRAY. - The type_name is the VARRAY type. - The type_name(...) is the constructor of the VARRAY type, which accepts a comma-separated list of elements as arguments. It has the same name as the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN NULL; END; / {code} Before using a VARRAY variable, it must be initialized. Otherwise, an error is raised: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN IF array_var(1) = 1 THEN NULL; END IF; END; / {code} {noformat} ORA-06531: Reference to uninitialized collection ORA-06512: at line 5 {noformat} The following syntax initialized a VARRAY variable to an empty collection (zero elements): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(); -- assignment initializing END; / {code} The following syntax specifies elements while initializing a VARRAY: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(1,2,3); -- assignment initializing END; / {code} A VARRAY can be assigneed to another VARRAY (all members are copied): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_var1; END; / {code} h2. Accessing VARRAY elements To access an array element use the following syntax: {code:sql} varray_name(n); {code} where {{n}} is the index of the element position between 1 and the maximum number of elements defined in the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN array_var(1):= array_var(1)*10; DBMS_OUTPUT.PUT_LINE(array_var(1)); END; / {code} {noformat} 10 {noformat} If the index is not in the range (1, initialized element count), the SUBSCRIPT_BEYOND_COUNT error is raised. {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(4)); END; / {code} {noformat} ORA-06533: Subscript beyond count ORA-06512: at line 5 {noformat} If the index is greater than the number of elements in the VARRAY type declaration, the SUBSCRIPT_BEYOND_COUNT error is also raised, but with a different error text: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(11)); END; / {code} {noformat} ORA-06532: Subscript outside of limit ORA-06512: at line 5 {noformat} h2. Adding elements The method EXTEND adds elements to the end of a VARRAY. - EXTEND appends one NULL element to the VARRAY - EXTEND(n) appends n NULL elements to the VARRAY - EXTEND(n,i) appends n copies of the i-th element to the collection. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(10); BEGIN array_var.EXTEND; array_var(2):= 20; array_var.EXTEND(2); array_var(3):= 30; array_var(4):= 40; array_var.EXTEND(2,1); FOR i IN 1..6 LOOP DBMS_OUTPUT.PUT_LINE(array_var(i)); END LOOP; END; / {code} {noformat} 10 20 30 40 10 10 {noformat} h2. Deleting elements To delete all elements of a VARRAY, you use the DELETE method: {code:sql} array_var.DELETE; {code} To remove one element from the end of a VARRAY, use the TRIM() method: {code:sql} array_var.TRIM() {code} To remove n elements from the end of a VARRAY, use the TRIM(n) method: {code:sql} array_var.TRIM(n) {code} h2. TODO: Describe other methods: - EXISTS - FIRST - LAST - COUNT - LIMIT |
Description |
This task is for using Oracle style VARRAYs inside stored routines.
A VARRAY is single-dimensional collections of elements with the same data type. A VARRAY always has a fixed number of elements and never has gaps between the elements (not sparse). h2. Declare a VARRAY type {code:sql} TYPE array_type_name IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} - array_type_name is the type of the VARRAY. - max_elements is the maximum number of elements allowed in the VARRAY. - NOT NULL specifies that the element of the VARRAY of that type cannot have NULL elements. Note that a VARRAY variable can be null, or uninitialized. - element_type is the type of elements of the VARRAY type’s variable. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; BEGIN NULL; END; / {code} h2. Declare a global VARRAY type To create a VARRAY type which is accessible globally in the database, not just in PL/SQL code, one uses the following syntax: {code:sql} CREATE [OR REPLACE ] TYPE type_name AS | IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} Global VARRAY types is out of scope of this task and will be implemented separately. h2. Declare and initialize VARRAY variables Once a VARRAY type is created, a VARRAY instance of that type can be declared. The syntax is: {code:sql} varray_name type_name [:= type_name(...)]; {code} - The varray_name is the name of the VARRAY. - The type_name is the VARRAY type. - The type_name(...) is the constructor of the VARRAY type, which accepts a comma-separated list of elements as arguments. It has the same name as the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN NULL; END; / {code} Before using a VARRAY variable, it must be initialized. Otherwise, an error is raised: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN IF array_var(1) = 1 THEN NULL; END IF; END; / {code} {noformat} ORA-06531: Reference to uninitialized collection ORA-06512: at line 5 {noformat} The following syntax initialized a VARRAY variable to an empty collection (zero elements): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(); -- assignment initializing END; / {code} The following syntax specifies elements while initializing a VARRAY: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(1,2,3); -- assignment initializing END; / {code} A VARRAY can be assigneed to another VARRAY (all members are copied): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_var1; END; / {code} h2. Accessing VARRAY elements To access an array element use the following syntax: {code:sql} varray_name(n); {code} where {{n}} is the index of the element position between 1 and the maximum number of elements defined in the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN array_var(1):= array_var(1)*10; DBMS_OUTPUT.PUT_LINE(array_var(1)); END; / {code} {noformat} 10 {noformat} If the index is not in the range (1, initialized element count), the SUBSCRIPT_BEYOND_COUNT error is raised. {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(4)); END; / {code} {noformat} ORA-06533: Subscript beyond count ORA-06512: at line 5 {noformat} If the index is greater than the number of elements in the VARRAY type declaration, the SUBSCRIPT_BEYOND_COUNT error is also raised, but with a different error text: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(11)); END; / {code} {noformat} ORA-06532: Subscript outside of limit ORA-06512: at line 5 {noformat} h2. Adding elements The method EXTEND adds elements to the end of a VARRAY. - EXTEND appends one NULL element to the VARRAY - EXTEND(n) appends n NULL elements to the VARRAY - EXTEND(n,i) appends n copies of the i-th element to the collection. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(10); BEGIN array_var.EXTEND; array_var(2):= 20; array_var.EXTEND(2); array_var(3):= 30; array_var(4):= 40; array_var.EXTEND(2,1); FOR i IN 1..6 LOOP DBMS_OUTPUT.PUT_LINE(array_var(i)); END LOOP; END; / {code} {noformat} 10 20 30 40 10 10 {noformat} h2. Deleting elements To delete all elements of a VARRAY, you use the DELETE method: {code:sql} array_var.DELETE; {code} To remove one element from the end of a VARRAY, use the TRIM() method: {code:sql} array_var.TRIM() {code} To remove n elements from the end of a VARRAY, use the TRIM(n) method: {code:sql} array_var.TRIM(n) {code} h2. TODO: Describe other methods: - EXISTS - FIRST - LAST - COUNT - LIMIT |
This task is for using Oracle style VARRAYs inside stored routines.
A VARRAY is single-dimensional collections of elements with the same data type. A VARRAY always has a fixed number of elements and never has gaps between the elements (not sparse). h2. Declare a VARRAY type {code:sql} TYPE array_type_name IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} - array_type_name is the type of the VARRAY. - max_elements is the maximum number of elements allowed in the VARRAY. - NOT NULL specifies that the element of the VARRAY of that type cannot have NULL elements. Note that a VARRAY variable can be null, or uninitialized. - element_type is the type of elements of the VARRAY type’s variable. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; BEGIN NULL; END; / {code} h2. Declare a global VARRAY type To create a VARRAY type which is accessible globally in the database, not just in PL/SQL code, one uses the following syntax: {code:sql} CREATE [OR REPLACE ] TYPE type_name AS | IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} Global VARRAY types is out of scope of this task and will be implemented separately. h2. Declare and initialize VARRAY variables Once a VARRAY type is created, a VARRAY instance of that type can be declared. The syntax is: {code:sql} varray_name type_name [:= type_name(...)]; {code} - The varray_name is the name of the VARRAY. - The type_name is the VARRAY type. - The type_name(...) is the constructor of the VARRAY type, which accepts a comma-separated list of elements as arguments. It has the same name as the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN NULL; END; / {code} Before using a VARRAY variable, it must be initialized. Otherwise, an error is raised: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN IF array_var(1) = 1 THEN NULL; END IF; END; / {code} {noformat} ORA-06531: Reference to uninitialized collection ORA-06512: at line 5 {noformat} The following syntax initialized a VARRAY variable to an empty collection (zero elements): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(); -- assignment initializing END; / {code} The following syntax specifies elements while initializing a VARRAY: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(1,2,3); -- assignment initializing END; / {code} A VARRAY can be assigneed to another VARRAY (all members are copied): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_var1; END; / {code} h2. Accessing VARRAY elements To access an array element use the following syntax: {code:sql} varray_name(n); {code} where {{n}} is the index of the element position between 1 and the maximum number of elements defined in the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN array_var(1):= array_var(1)*10; DBMS_OUTPUT.PUT_LINE(array_var(1)); END; / {code} {noformat} 10 {noformat} If the index is not in the range (1, initialized element count), the SUBSCRIPT_BEYOND_COUNT error is raised. {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(4)); END; / {code} {noformat} ORA-06533: Subscript beyond count ORA-06512: at line 5 {noformat} If the index is greater than the number of elements in the VARRAY type declaration, the SUBSCRIPT_BEYOND_COUNT error is also raised, but with a different error text: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(11)); END; / {code} {noformat} ORA-06532: Subscript outside of limit ORA-06512: at line 5 {noformat} h2. Adding elements The method EXTEND adds elements to the end of a VARRAY. - {{EXTEND}} appends one {{NULL}} element to the VARRAY - {{EXTEND(n)}} appends {{n}} {{NULL}} elements to the VARRAY - {{EXTEND(n,i)}} appends {{n}} copies of the {{i}}-th element to the collection. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(10); BEGIN array_var.EXTEND; array_var(2):= 20; array_var.EXTEND(2); array_var(3):= 30; array_var(4):= 40; array_var.EXTEND(2,1); FOR i IN 1..6 LOOP DBMS_OUTPUT.PUT_LINE(array_var(i)); END LOOP; END; / {code} {noformat} 10 20 30 40 10 10 {noformat} h2. Deleting elements To delete all elements of a VARRAY, you use the DELETE method: {code:sql} array_var.DELETE; {code} To remove one element from the end of a VARRAY, use the TRIM() method: {code:sql} array_var.TRIM() {code} To remove n elements from the end of a VARRAY, use the TRIM(n) method: {code:sql} array_var.TRIM(n) {code} h2. TODO: Describe other methods: - EXISTS - FIRST - LAST - COUNT - LIMIT |
Description |
This task is for using Oracle style VARRAYs inside stored routines.
A VARRAY is single-dimensional collections of elements with the same data type. A VARRAY always has a fixed number of elements and never has gaps between the elements (not sparse). h2. Declare a VARRAY type {code:sql} TYPE array_type_name IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} - array_type_name is the type of the VARRAY. - max_elements is the maximum number of elements allowed in the VARRAY. - NOT NULL specifies that the element of the VARRAY of that type cannot have NULL elements. Note that a VARRAY variable can be null, or uninitialized. - element_type is the type of elements of the VARRAY type’s variable. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; BEGIN NULL; END; / {code} h2. Declare a global VARRAY type To create a VARRAY type which is accessible globally in the database, not just in PL/SQL code, one uses the following syntax: {code:sql} CREATE [OR REPLACE ] TYPE type_name AS | IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} Global VARRAY types is out of scope of this task and will be implemented separately. h2. Declare and initialize VARRAY variables Once a VARRAY type is created, a VARRAY instance of that type can be declared. The syntax is: {code:sql} varray_name type_name [:= type_name(...)]; {code} - The varray_name is the name of the VARRAY. - The type_name is the VARRAY type. - The type_name(...) is the constructor of the VARRAY type, which accepts a comma-separated list of elements as arguments. It has the same name as the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN NULL; END; / {code} Before using a VARRAY variable, it must be initialized. Otherwise, an error is raised: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN IF array_var(1) = 1 THEN NULL; END IF; END; / {code} {noformat} ORA-06531: Reference to uninitialized collection ORA-06512: at line 5 {noformat} The following syntax initialized a VARRAY variable to an empty collection (zero elements): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(); -- assignment initializing END; / {code} The following syntax specifies elements while initializing a VARRAY: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(1,2,3); -- assignment initializing END; / {code} A VARRAY can be assigneed to another VARRAY (all members are copied): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_var1; END; / {code} h2. Accessing VARRAY elements To access an array element use the following syntax: {code:sql} varray_name(n); {code} where {{n}} is the index of the element position between 1 and the maximum number of elements defined in the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN array_var(1):= array_var(1)*10; DBMS_OUTPUT.PUT_LINE(array_var(1)); END; / {code} {noformat} 10 {noformat} If the index is not in the range (1, initialized element count), the SUBSCRIPT_BEYOND_COUNT error is raised. {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(4)); END; / {code} {noformat} ORA-06533: Subscript beyond count ORA-06512: at line 5 {noformat} If the index is greater than the number of elements in the VARRAY type declaration, the SUBSCRIPT_BEYOND_COUNT error is also raised, but with a different error text: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(11)); END; / {code} {noformat} ORA-06532: Subscript outside of limit ORA-06512: at line 5 {noformat} h2. Adding elements The method EXTEND adds elements to the end of a VARRAY. - {{EXTEND}} appends one {{NULL}} element to the VARRAY - {{EXTEND(n)}} appends {{n}} {{NULL}} elements to the VARRAY - {{EXTEND(n,i)}} appends {{n}} copies of the {{i}}-th element to the collection. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(10); BEGIN array_var.EXTEND; array_var(2):= 20; array_var.EXTEND(2); array_var(3):= 30; array_var(4):= 40; array_var.EXTEND(2,1); FOR i IN 1..6 LOOP DBMS_OUTPUT.PUT_LINE(array_var(i)); END LOOP; END; / {code} {noformat} 10 20 30 40 10 10 {noformat} h2. Deleting elements To delete all elements of a VARRAY, you use the DELETE method: {code:sql} array_var.DELETE; {code} To remove one element from the end of a VARRAY, use the TRIM() method: {code:sql} array_var.TRIM() {code} To remove n elements from the end of a VARRAY, use the TRIM(n) method: {code:sql} array_var.TRIM(n) {code} h2. TODO: Describe other methods: - EXISTS - FIRST - LAST - COUNT - LIMIT |
This task is for using Oracle style VARRAYs inside stored routines.
A VARRAY is single-dimensional collections of elements with the same data type. A VARRAY always has a fixed number of elements and never has gaps between the elements (not sparse). h2. Declare a VARRAY type {code:sql} TYPE array_type_name IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} - array_type_name is the type of the VARRAY. - max_elements is the maximum number of elements allowed in the VARRAY. - NOT NULL specifies that the element of the VARRAY of that type cannot have NULL elements. Note that a VARRAY variable can be null, or uninitialized. - element_type is the type of elements of the VARRAY type’s variable. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; BEGIN NULL; END; / {code} h2. Declare a global VARRAY type To create a VARRAY type which is accessible globally in the database, not just in PL/SQL code, one uses the following syntax: {code:sql} CREATE [OR REPLACE ] TYPE type_name AS | IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} Global VARRAY types is out of scope of this task and will be implemented separately. h2. Declare and initialize VARRAY variables Once a VARRAY type is created, a VARRAY instance of that type can be declared. The syntax is: {code:sql} varray_name type_name [:= type_name(...)]; {code} - The varray_name is the name of the VARRAY. - The type_name is the VARRAY type. - The type_name(...) is the constructor of the VARRAY type, which accepts a comma-separated list of elements as arguments. It has the same name as the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN NULL; END; / {code} Before using a VARRAY variable, it must be initialized. Otherwise, an error is raised: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN IF array_var(1) = 1 THEN NULL; END IF; END; / {code} {noformat} ORA-06531: Reference to uninitialized collection ORA-06512: at line 5 {noformat} The following syntax initialized a VARRAY variable to an empty collection (zero elements): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(); -- assignment initializing END; / {code} The following syntax specifies elements while initializing a VARRAY: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(1,2,3); -- assignment initializing END; / {code} A VARRAY can be assigneed to another VARRAY (all members are copied): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_var1; END; / {code} h2. Accessing VARRAY elements To access an array element use the following syntax: {code:sql} varray_name(n); {code} where {{n}} is the index of the element position between 1 and the maximum number of elements defined in the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN array_var(1):= array_var(1)*10; DBMS_OUTPUT.PUT_LINE(array_var(1)); END; / {code} {noformat} 10 {noformat} If the index is not in the range (1, initialized element count), the SUBSCRIPT_BEYOND_COUNT error is raised. {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(4)); END; / {code} {noformat} ORA-06533: Subscript beyond count ORA-06512: at line 5 {noformat} If the index is greater than the number of elements in the VARRAY type declaration, the SUBSCRIPT_BEYOND_COUNT error is also raised, but with a different error text: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(11)); END; / {code} {noformat} ORA-06532: Subscript outside of limit ORA-06512: at line 5 {noformat} h2. Adding elements The method EXTEND adds elements to the end of a VARRAY. - {{EXTEND}} appends one {{NULL}} element to the VARRAY - {{EXTEND (n)}} appends {{n}} {{NULL}} elements to the VARRAY - {{EXTEND(n,i)}} appends {{n}} copies of the {{i}}-th element to the collection. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(10); BEGIN array_var.EXTEND; array_var(2):= 20; array_var.EXTEND(2); array_var(3):= 30; array_var(4):= 40; array_var.EXTEND(2,1); FOR i IN 1..6 LOOP DBMS_OUTPUT.PUT_LINE(array_var(i)); END LOOP; END; / {code} {noformat} 10 20 30 40 10 10 {noformat} h2. Deleting elements To delete all elements of a VARRAY, you use the DELETE method: {code:sql} array_var.DELETE; {code} To remove one element from the end of a VARRAY, use the TRIM() method: {code:sql} array_var.TRIM() {code} To remove n elements from the end of a VARRAY, use the TRIM(n) method: {code:sql} array_var.TRIM(n) {code} h2. TODO: Describe other methods: - EXISTS - FIRST - LAST - COUNT - LIMIT |
Description |
This task is for using Oracle style VARRAYs inside stored routines.
A VARRAY is single-dimensional collections of elements with the same data type. A VARRAY always has a fixed number of elements and never has gaps between the elements (not sparse). h2. Declare a VARRAY type {code:sql} TYPE array_type_name IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} - array_type_name is the type of the VARRAY. - max_elements is the maximum number of elements allowed in the VARRAY. - NOT NULL specifies that the element of the VARRAY of that type cannot have NULL elements. Note that a VARRAY variable can be null, or uninitialized. - element_type is the type of elements of the VARRAY type’s variable. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; BEGIN NULL; END; / {code} h2. Declare a global VARRAY type To create a VARRAY type which is accessible globally in the database, not just in PL/SQL code, one uses the following syntax: {code:sql} CREATE [OR REPLACE ] TYPE type_name AS | IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} Global VARRAY types is out of scope of this task and will be implemented separately. h2. Declare and initialize VARRAY variables Once a VARRAY type is created, a VARRAY instance of that type can be declared. The syntax is: {code:sql} varray_name type_name [:= type_name(...)]; {code} - The varray_name is the name of the VARRAY. - The type_name is the VARRAY type. - The type_name(...) is the constructor of the VARRAY type, which accepts a comma-separated list of elements as arguments. It has the same name as the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN NULL; END; / {code} Before using a VARRAY variable, it must be initialized. Otherwise, an error is raised: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN IF array_var(1) = 1 THEN NULL; END IF; END; / {code} {noformat} ORA-06531: Reference to uninitialized collection ORA-06512: at line 5 {noformat} The following syntax initialized a VARRAY variable to an empty collection (zero elements): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(); -- assignment initializing END; / {code} The following syntax specifies elements while initializing a VARRAY: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(1,2,3); -- assignment initializing END; / {code} A VARRAY can be assigneed to another VARRAY (all members are copied): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_var1; END; / {code} h2. Accessing VARRAY elements To access an array element use the following syntax: {code:sql} varray_name(n); {code} where {{n}} is the index of the element position between 1 and the maximum number of elements defined in the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN array_var(1):= array_var(1)*10; DBMS_OUTPUT.PUT_LINE(array_var(1)); END; / {code} {noformat} 10 {noformat} If the index is not in the range (1, initialized element count), the SUBSCRIPT_BEYOND_COUNT error is raised. {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(4)); END; / {code} {noformat} ORA-06533: Subscript beyond count ORA-06512: at line 5 {noformat} If the index is greater than the number of elements in the VARRAY type declaration, the SUBSCRIPT_BEYOND_COUNT error is also raised, but with a different error text: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(11)); END; / {code} {noformat} ORA-06532: Subscript outside of limit ORA-06512: at line 5 {noformat} h2. Adding elements The method EXTEND adds elements to the end of a VARRAY. - {{EXTEND}} appends one {{NULL}} element to the VARRAY - {{EXTEND (n)}} appends {{n}} {{NULL}} elements to the VARRAY - {{EXTEND(n,i)}} appends {{n}} copies of the {{i}}-th element to the collection. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(10); BEGIN array_var.EXTEND; array_var(2):= 20; array_var.EXTEND(2); array_var(3):= 30; array_var(4):= 40; array_var.EXTEND(2,1); FOR i IN 1..6 LOOP DBMS_OUTPUT.PUT_LINE(array_var(i)); END LOOP; END; / {code} {noformat} 10 20 30 40 10 10 {noformat} h2. Deleting elements To delete all elements of a VARRAY, you use the DELETE method: {code:sql} array_var.DELETE; {code} To remove one element from the end of a VARRAY, use the TRIM() method: {code:sql} array_var.TRIM() {code} To remove n elements from the end of a VARRAY, use the TRIM(n) method: {code:sql} array_var.TRIM(n) {code} h2. TODO: Describe other methods: - EXISTS - FIRST - LAST - COUNT - LIMIT |
This task is for using Oracle style VARRAYs inside stored routines.
A VARRAY is single-dimensional collections of elements with the same data type. A VARRAY always has a fixed number of elements and never has gaps between the elements (not sparse). h2. Declare a VARRAY type {code:sql} TYPE array_type_name IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} - array_type_name is the type of the VARRAY. - max_elements is the maximum number of elements allowed in the VARRAY. - NOT NULL specifies that the element of the VARRAY of that type cannot have NULL elements. Note that a VARRAY variable can be null, or uninitialized. - element_type is the type of elements of the VARRAY type’s variable. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; BEGIN NULL; END; / {code} h2. Declare a global VARRAY type To create a VARRAY type which is accessible globally in the database, not just in PL/SQL code, one uses the following syntax: {code:sql} CREATE [OR REPLACE ] TYPE type_name AS | IS VARRAY(max_elements) OF element_type [NOT NULL]; {code} Global VARRAY types is out of scope of this task and will be implemented separately. h2. Declare and initialize VARRAY variables Once a VARRAY type is created, a VARRAY instance of that type can be declared. The syntax is: {code:sql} varray_name type_name [:= type_name(...)]; {code} - The varray_name is the name of the VARRAY. - The type_name is the VARRAY type. - The type_name(...) is the constructor of the VARRAY type, which accepts a comma-separated list of elements as arguments. It has the same name as the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN NULL; END; / {code} Before using a VARRAY variable, it must be initialized. Otherwise, an error is raised: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name; BEGIN IF array_var(1) = 1 THEN NULL; END IF; END; / {code} {noformat} ORA-06531: Reference to uninitialized collection ORA-06512: at line 5 {noformat} The following syntax initialized a VARRAY variable to an empty collection (zero elements): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(); -- assignment initializing END; / {code} The following syntax specifies elements while initializing a VARRAY: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_type_name(1,2,3); -- assignment initializing END; / {code} A VARRAY can be assigneed to another VARRAY (all members are copied): {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var1 array_type_name := array_type_name(1,2,3); -- declare initializing array_var2 array_type_name; BEGIN array_var2 := array_var1; END; / {code} h2. Accessing VARRAY elements To access an array element use the following syntax: {code:sql} varray_name(n); {code} where {{n}} is the index of the element position between 1 and the maximum number of elements defined in the VARRAY type. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN array_var(1):= array_var(1)*10; DBMS_OUTPUT.PUT_LINE(array_var(1)); END; / {code} {noformat} 10 {noformat} If the index is not in the range (1, initialized element count), the SUBSCRIPT_BEYOND_COUNT error is raised. {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(4)); END; / {code} {noformat} ORA-06533: Subscript beyond count ORA-06512: at line 5 {noformat} If the index is greater than the number of elements in the VARRAY type declaration, the SUBSCRIPT_BEYOND_COUNT error is also raised, but with a different error text: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(1,2,3); BEGIN DBMS_OUTPUT.PUT_LINE(array_var(11)); END; / {code} {noformat} ORA-06532: Subscript outside of limit ORA-06512: at line 5 {noformat} h2. Adding elements The method EXTEND adds elements to the end of a VARRAY. - {{EXTEND}} appends one {{NULL}} element to the VARRAY - {{EXTEND\(n\)}} appends {{n}} {{NULL}} elements to the VARRAY - {{EXTEND(n,i)}} appends {{n}} copies of the {{i}}-th element to the collection. Example: {code:sql} DECLARE TYPE array_type_name IS VARRAY(10) OF INT; array_var array_type_name := array_type_name(10); BEGIN array_var.EXTEND; array_var(2):= 20; array_var.EXTEND(2); array_var(3):= 30; array_var(4):= 40; array_var.EXTEND(2,1); FOR i IN 1..6 LOOP DBMS_OUTPUT.PUT_LINE(array_var(i)); END LOOP; END; / {code} {noformat} 10 20 30 40 10 10 {noformat} h2. Deleting elements To delete all elements of a VARRAY, you use the DELETE method: {code:sql} array_var.DELETE; {code} To remove one element from the end of a VARRAY, use the TRIM() method: {code:sql} array_var.TRIM() {code} To remove n elements from the end of a VARRAY, use the TRIM(n) method: {code:sql} array_var.TRIM(n) {code} h2. TODO: Describe other methods: - EXISTS - FIRST - LAST - COUNT - LIMIT |
Remote Link | This issue links to "VARRAY documentation at Oracle (Web Link)" [ 36251 ] |
Fix Version/s | 11.5 [ 29506 ] | |
Fix Version/s | 11.4 [ 29301 ] |
Fix Version/s | 11.6 [ 29515 ] | |
Fix Version/s | 11.5 [ 29506 ] |
Fix Version/s | 11.6 [ 29515 ] |
Link | This issue relates to MDEV-34319 [ MDEV-34319 ] |
Link | This issue relates to MDEV-10143 [ MDEV-10143 ] |
Link | This issue relates to MDEV-35980 [ MDEV-35980 ] |