oracle composite data type

Nested table definition:

TYPE type_naem AS TABLE OF element_type[NOT NULL]

Nested table built-in functions:

  1. Use the constructor to initialize (can take elements)

  2.EXTEND============Add an element at the end of the collection

     EXTEND(x) ===== adds x elements to the end of the collection

  3.COUNT========== returns the number of elements in the collection

Nested tables hold data:

  1. The constructor is initialized: type_naem variable:=type_naem ('element','element',...)

  2.: type_naem variable(i):= value;

Nested table to get data:

  type_naem variable (i)

 

The following is reproduced from itpub, the essence! ! Must see below! ! (Original address: http://www.itpub.net/thread-610297-1-1.html )

--This document can be copied and run directly.

/*
oracle composite data types
PL/SQL has two composite data structures: records and sets. Records are composed of different fields, and sets are composed of different elements.
*/

/*
First, the record type is

similar to the structure in the C language, and there are two definitions: explicit definition and implicit definition.
*/

create table test
(
id varchar2(20),
mc varchar2(60)
);

insert into test values('111','11111');
insert into test values('222','22222');
insert into test values('333','33333');
insert into test values('444','44444');
insert into test values('555','55555');
insert into test values('666','66666 ');

commit;

/*

1.







id test.id%type,
mc test.mc%type
);
var_record t_record;
counter number default 0;
begin
        for row_test in (select id,mc from test) loop
                counter := counter + 1;
                var_record.id := row_test.id;
                var_record.mc := row_test.mc;
                dbms_output.put_line('var_record:'||var_record.id||'---'||var_record.mc);
                dbms_output.put_line('row_test:'||row_test.id||'---'||row_test.mc);
                dbms_output.put_line('================loop '||counter||' times.');
        end loop;
        exception when others then
                dbms_output.put_line(sqlcode||sqlerrm);
end;
//* There are some PL /

SQL directives that do not use the %ROWTYPE attribute when using implicitly defined records, such as ld and :new records in cursor FOR loops or triggers. */

/*

1.2. Implicitly defined records

*/

In the implicitly defined records, we do not need to describe each field of the record. When declaring record variables, use the %ROWTYPE command to define records with the same structure as database tables, views, and cursors.

declare
t_record1 test%rowtype;
cursor cur_test(v_id in varchar2) is
select id,mc from test
where id <= v_id;
t_record2 cur_test%rowtype;
begin
        for row_test in cur_test('333') loop
                t_record1.id := row_test.id ;
                t_record1.mc := row_test.mc;
                t_record2.id := row_test.id;
                t_record2.mc := row_test.id;
                dbms_output.put_line('t_record1:'||t_record1.id||'---'|| t_record1.mc);
                dbms_output.put_line('t_record2:'||t_record2.id||'---'||t_record2.mc);
                dbms_output.put_line('row_test:'||row_test.id||'---'||row_test .mc);
                dbms_output.put_line('================loop '||cur_test%rowcount||' times.');
        end loop;
        exception when others then
                dbms_output.put_line (sqlcode||sqlerrm);
end;
/

/*

Second, the collection is

similar to the array in the C language. In ORACLE7.3 and earlier versions, there is only one collection called PL/SQL table. This type of collection is still reserved. Is the index (INDEX_BY) table.
PL/SQL has three types of collections

a, Index_by table
b, nested table
c, VARRAY


The differences between the three types of collections include data binding, sparsity, and storage capacity in the database.

Data Binding:
        Binding involves the limitation of the number of elements in the collection. The number of elements in the VARRAY collection is limited, and Index_by and nested tables are not limited.
Sparsity (sparsity): Sparsity
        describes whether the subscripts of the collection have intervals. Index_by tables and nested tables can be sparse, while VARRAY type collections are compact, and there is no interval between its subscripts.
Storage:
        Index_by tables cannot be stored in the database, but nested tables and VARRAYs can be stored in the database.
        
Index_by table definition syntax is as follows:
        TYPE type_name IS TABLE OF element_type [NOT NULL] INDEX BY BINARY_INTEGER;
        the keyword is INDEX BY BINARY_INTEGER, without this keyword, the collection will be a nested table. Since it is not stored in the database,
        element_type can be any valid PL/SQL data type, including: PLS/INTEGER, SIGNTYPE, and BOOLEAN.

The nested table definition syntax is as follows:
        Nested tables are very similar to Index_by tables, and the syntax for creation is also very similar. There is just no INDEX BY BINARY_INTEGER substring
        TYPE type_name IS TABLE OF element_type [NOT NULL];
        Nested tables stored in a database are not stored in the same data block as other data in the table, they are actually stored in the first in two tables.
        Nested tables fetched from the database also do not guarantee the order of the elements. Collection data is stored offline, so nested tables are suitable for large collections.
        
VARRAY definition syntax is as follows:
        TYPE type_name IS [VARRAY|VARYING ARRAY] (max_size) OF element_type [NOT NULL];
                max_size is an integer used to indicate the maximum number of elements that a VARRAY set has. The number of elements in the VARRAY collection can be less than max_size, but not more than max_size.
                element_type is the data type of a one-dimensional element. If element_type is a record, then this record can only use scalar data fields (similar to nested subscripts).
                When VARRAY is stored in the database, it is stored in the same data block as other data in the table, and the order of elements is stored in VARRAY.
                Collections are stored online, and VARRAY is great for small collections.
                
Both nested tables and VARRAYs can be stored as columns in the database table, so the collection itself can be NULL, and when the collection is NULL, the user cannot refer to the elements in the collection.
Users can use the IS NULL operator to check if a collection is NULL.

1. index_by table:

*/

declare
cursor cur_test is select id,mc from test;
type t_test1 is table of varchar2(60) index by binary_integer;
type t_test2 is table of test%rowtype index by binary_integer;
var_test1 t_test1;
var_test2 t_test2;
var_new t_test2;
begin
SELECT id,mc INTO var_test2(0) FROM test WHERE id='111';
dbms_output.put_line('var_test2(0):'||var_test2(0).id||'---'||var_test2(0).mc);
SELECT id,mc INTO var_test2(8) FROM test WHERE id='333';
dbms_output.put_line('var_test2(8):'||var_test2(8).id||'---'||var_test2(8).mc);
var_new := var_test2;
dbms_output.put_line('=====  copy var_test2 to var_new  =====');
dbms_output.put_line('var_new(0):'||var_new(0).id||'---'||var_new(0).mc);
dbms_output.put_line('var_new(8):'||var_new(8).id||'---'||var_new(8).mc);
end;
/

/*
Nested tables and VARRAYs are initialized by constructor

2. Nested tables and VARRAYs:

*/

DECLARE 
TYPE t_test1 IS TABLE OF test.id%TYPE;
TYPE t_test2 IS VARRAY (10) OF test.id%TYPE;
var_test1 t_test1;
var_test2 t_test2 ;
begin
        --var_test1(1) := ('test1.1'); -- cannot be assigned without initialization
        var_test1 := t_test1('test1.1','test1.2','test1.3');
        dbms_output. put_line('var_test1: '||var_test1(1)||','||var_test1(2)||','||var_test1(3));
        var_test2 := t_test2('test2.1','test2. 2','test2.3');
        dbms_output.put_line('var_test2: '||var_test2(1)||','||var_test2(2)||','||var_test2(3));
        var_test1( 2) := 'test1.2_update';
        dbms_output.put_line('==== modified var_test1(2) ====');
        dbms_output.put_line('var_test1: '||var_test1(1)||','||var_test1(2)||','||var_test1(3));
        dbms_output.put_line(var_test1.next(3));
        dbms_output.put_line('var_test2 number of elements: '||var_test2.limit());
end;
/

/*
The elements of the nested table can be sets, note that the assignment is in the form of varray_element.record_column :=.
Except for the constructor In addition, there are many built-in functions for collections, which are called methods according to the object-oriented method.
Method =========Description ======================================= =============================Use Limit
COUNT========== Returns the number of elements in the collection   
DELETE ========Delete all elements in the set  
DELETE(x)======Delete the element whose subscript is x====================== ============================== Illegal
DELETE(x,y)=== delete element subscript from x to VARRAY Elements of Y ================================================= =Illegal for VARRAY
EXIST(x)====== Returns TRUE if set element x has been initialized, FALSE otherwise  
EXTEND========Add an element at the end of the collection =================================== ==============Illegal
EXTEND(x) for Index_by =====Add x elements to the end of the set ================= =================================== Illegal for Index_by
EXTEND(x,n)=== at the end of the set Add x copies of element n =========================================== = Illegal FIRST for Index_by
========= Returns the index of the first element in the collection, always returns 1 for VARRAY collections.  
LAST==========Returns the index of the last element in the collection, for VARRAY the return value is always equal to COUNT.   
LIMIT==========Returns the maximum number of elements in the VARRY collection== ========================================== Index_by collections and nested tables are useless
NEXT( x)======== returns the value of the element after and next to the xth element, or null if x is the last element.  
PRIOR(x)====== returns the value at the xth element value of the element immediately preceding it, or null if x is the first element.  
TRIM==========Remove an element from the end of the set================================== ============= is not legal for index_by
TRIM(x)========Remove x elements from the end of the set =============================== =============== is not legal for index_by

*/

/*

Third, the usage of BULK COLLECT in a comprehensive example 

*/

set serverout on
DECLARE
TYPE t_record IS RECORD (
id number(18,0) ,
mc varchar2(50)
);
var_record t_record;
type t_test is table of t_record;
var_test t_test := t_test();
cursor cur_test is select id,mc from test;
begin
open cur_test;
fetch cur_test BULK COLLECT INTO var_test;
for i in 1..var_test.count() loop
        dbms_output.put_line(var_test(i).id||'---'||var_test(i).mc);
end loop;
end;
/

 

/*

 

An example of an oracle function returning an array

Today, I wrote an example of a function returning an array to a friend, and posted it by the way.

create or replace type t_ret_table is table of varchar2(20);

create or replace function f_test(var_num in integer) return t_ret_table is
var_out t_ret_table;
begin
        var_out := t_ret_table();
        var_out.extend(var_num);
        for i in 1.. var_num loop
                var_out(i) := i;
        end loop;
        return var_out;
end f_test;
/



set serverout on
declare
aa t_ret_table;
begin
aa := f_test(10);
for i in 1..aa.count loop
        dbms_output.put_line( aa(i));
end loop;
end;
/
 
 
 
   

*/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325078672&siteId=291194637