Oracle Array Learning small knowledge also need to accumulate, develop good learning attitude _oracle

Source: Internet
Author: User
Tags arrays extend oracle documentation
A code snippet to help explain.
The code snippet is as follows:
Copy Code code as follows:

Declare
Type T_indexby is table of number
Index by Binary_integer;
Type T_nesteed is table of number;
Type T_varray is Varray (a) of number;
V_indexby T_indexby;
V_nested t_nested;
V_varray T_varray;
Begin
V_indexby (1): = 1;
V_indexby (2): = 2;
V_nested:=t_nested (1,2,3,4,5);
V_varray:=t_varray (1,2);
End

A very simple sample code about an array in Oracle. Looking at this representative code, it is not the kind of passion that does not understand the type of array that you previously groped for in Oracle alone.
This code is also good, through a simple example of the main array types are listed in Le, good material is not stimulated by my answer to write the desire to write, so also generous finger force, summed up a bit, but also by the way to fill the gaps I have forgotten to summarize
This code, acquisition, how arrays are used in Oracle
1. Index by Table
2. Nested table
3. Varray variable Array
Here is an introduction to the three types of arrays in Oracle documentation
An index-by table is the most flexible and generally best-performing collection type for use inside pl/sql.
A nested table is appropriate for large collections this application stores and retrieves in portions.
A Varray is appropriate for small collections this application stores and retrieves in their.
Here is the application of the three of them, as if not to give us too direct impact, or let us first understand it, here the application of the embodiment of the things also good understanding le.
The sample code begins with the definition of three different types.
Type T_indexby is table of number index by Binary_integer; --Indexed by table
Type T_nesteed is table of number; --Nested table
Type T_varray is Varray (a) of number; --Varray
There is a distinct difference between the last two sentences and the latter one, no length defined, and Varray defines le length. Varray has a length limit, the access is over the length of the words will prompt the error of the bounds. The indexed by table and nested table obviously do not have this limitation, but for indexed by table and nested table, they are both different.
The latter part of sample above describes the difference between the two, for the index by table, here has specified the type of index, directly with the index type of variables indexed to identify each element, without the need to expand the size. This feature is somewhat like a map in Java (the difference is that the key here is sequential), and the nested table can be exactly the same as the list
We'll take a look at sample.
V_indexby (1): = 1;
V_indexby (2): = 2;
Here are two elements in V_indexby, 1, 1 and 2, 2, note that here (1), (2) and the back nested table is already varray,
Here, I interpret it as a key, not an element's ordinal number. So the index by here subscript, is not necessarily continuous, you can jump, and the other two are different, the others are true to the array object, subscript represents the element ordinal, and Java different, starting from 1.
V_nested:=t_nested (1,2,3,4,5);
V_varray:=t_varray (1,2);
This is the array that defines 5 and 2 elements, respectively.
V_nested:=t_nested (1,2,3,4,5); 5 Elements with a value of 1,2,3,4,5
V_varray:=t_varray (1,2); 2 element values are 1,2
To emphasize, for nested table, you need to use extend to extend the array, add elements, and Varrray do not need (already know the length, the time of definition).
V_nested.extend; V_nested (v_nested.count): = 6;
Everyone here can basically see their differences, the index by table in the structure and nested table and Varray are essentially different, then it must be different when used. Because the index by table subscript is not a serial number, so we can only access through key, here and Java is the same.
The above example, did not provide, and I found a lot of information on the internet did not give a detailed index by table traversal method, here I wrote a sample, for you to learn the reference
Copy Code code as follows:

Declare
Type T_array is Table of VARCHAR2 (1000) index by Binary_integer;
V_array T_array;
V_IDX number;
Begin
V_array (1): = ' A1 ';
V_array (2): = ' A2 ';
V_array ( -1): = ' A-1 '; --It's a key, so it can be negative.
V_idx: = V_array.first;
Loop
Exit when V_IDX is null;
Dbms_output.put_line (V_array (V_IDX));
V_idx: = V_array.next (V_IDX);
End Loop;
End
/

Results
A-1
A1
A2
Note that the method here, first, returns the Key,next () returns the next key
For nested table, the subscript is an ordinal number and cannot be negative.
Copy Code code as follows:

Declare
Type T_array is Table of VARCHAR2 (1000);
V_array T_array;
V_idx VARCHAR2 (1000);
Begin
V_array: = T_array (); ---Be sure to start with the initial words.
V_array.extend; ----Extended Array
V_array (1): = ' A1 ';
V_array.extend;
V_array (2): = ' A2 ';
V_array.extend;
V_array (3): = ' A-1 '; ----V_array ( -1): = ' A-1 '; There will be an error
V_idx: = V_array.first;
Loop
Dbms_output.put (v_idx| | ' ');
Exit when V_IDX is null;
Dbms_output.put_line (V_array (V_IDX));
V_idx: = V_array.next (V_IDX);
End Loop;
End
/

See nested tabled's access can also be the same as above, of course, you can also use a simpler way to traverse
Copy Code code as follows:

For I in 1..v_array.count loop
Dbms_output.put (i| | ' ');
Dbms_output.put_line (V_array (i));
End Loop;

In fact, you can see the visit to the list in Java is very similar.
This knowledge point, many people have written sample, the content is not many, but the small knowledge also must accumulate, quanxue "does not accumulate the Bujikuibu, does not have even the thousand miles, does not accumulate the small flow, does not have to become Jianghai", studies is this, and the Oracle study should be so. Before the lazy, I still have le today. "Go to the river, sooner or later to return."
Oracle's official introduction to index by array
http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28843/tdddg_procedures.htm
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.