Shanghai Tengke Education Dameng Database Training Dry Goods Sharing The Use of Collection Types in Dameng Database (Part 1)

During the use of DM PL/SQL, if we want to store a single data, we can store it by declaring variables. If we want to store a single row and multiple columns of data, we can use the record type (record), if we want To save data in a single column and multiple rows, you can use the collection type to store it. This article will introduce you to several collection types commonly used in Dameng database and their characteristics.

 

1. Index table

 

The characteristics of the index table:

 

1. You can use numbers or letters as subscripts

2. You can use negative numbers when using numbers as subscripts

3. It can only be used in a PL/SQL block and cannot be used as a table field

Let’s do some tests, first of all, the index table with numbers as subscripts

 

declare 

  type index_tab_type is table of varchar(30) index by int; 

  v_table index_tab_type; 

begin 

  v_table(-1):='hello';--Set the value of the element whose subscript is -1 

  v_table(1) :='and'; - set the value of the element whose subscript is 1 

  v_table(5):='world'; 

  print ('The number of elements is:'||v_table.count); 

  print ('The first element is'||v_table(v_table. first )); - output the first element 

   print ('The last element is'||v_table(v_table. last )); - output the last element

end

The execution results are as follows

 

 

Then there is an index table with letters as subscripts

declare 

  type index_tab_type is table of varchar(30) index by varchar(1); 

  v_table index_tab_type; 

begin 

  v_table('a'):='hello';--Set the value of the element whose subscript is -1 

  v_table('c'):='world'; 

  print ('Number of elements:'||v_table.count); 

  print ('first element'||v_table(v_table. first )); 

  print ('last element'||v_table(v_table. last )); 

end

Output result

 

 

It can be seen that when using characters as subscripts, the storage of the index table is similar to the storage of key-values

 

2. Varray (variable length array)

 

Features of Varray:

 

1. The maximum capacity must be declared when declaring.

2. Initialization is required for first use.

3. The actual size is 0 during initialization, and the extend method needs to be used to expand the upper limit to increase the element.

Here we change another way, save the names of 5 people with IDs 1001 to 1005 on the employee table in the sample library DMHR into this varray, the test is as follows

 

DECLARE

TYPE  MY_ARRAY_TYPE  IS  VARRAY (10)  OF  VARCHAR(100); - Declare a varray with a maximum capacity of 100

v MY_ARRAY_TYPE;

BEGIN

v:=MY_ARRAY_TYPE(); --Initialize varray

FOR IN 1..5 LOOP

v.EXTEND(); --Expansion limit

SELECT employee_name INTO v(I) FROM dmhr.employee

WHERE employee_id=1000+i;

END LOOP;

PRINT ' v.COUNT()='||v.COUNT();

FOR IN 1..v.COUNT() LOOP

PRINT 'v (' || i || ') =' || v (i);

END LOOP;

END;

Output result

 

 

It should be noted that when varray is used, extend must be used to expand the upper limit, otherwise there will be an error that the collection subscript is out of range.

 

Because of space reasons, the content of this issue is here. In the next issue of sharing, we will continue to introduce you to the use of nested tables and array types, so stay tuned.

Guess you like

Origin blog.csdn.net/qq_42726883/article/details/108528697