【Oracle】record varray (associative array 关联数组) table (nested table type 嵌套表类型)和%type、%rowtype的使用详解

 

 

官方文档:

https://docs.oracle.com/en/database/oracle/oracle-database/12.2/lnpls/plsql-data-types.html#GUID-391C58FD-16AF-486C-AF28-173E309CDBA5

PL/SQL Data Types

 

https://docs.oracle.com/en/database/oracle/oracle-database/12.2/lnpls/plsql-collections-and-records.html#GUID-8060F01F-B53B-48D4-9239-7EA8461C2170

PL/SQL Collections and Records

 

https://docs.oracle.com/en/database/oracle/oracle-database/12.2/lnpls/collection-variable.html#GUID-89A1863C-65A1-40CF-9392-86E9FDC21BE9

13.11 Collection Variable Declaration

A collection variable is a composite variable whose internal components, called elements, have the same data type.

The value of a collection variable and the values of its elements can change.

You reference an entire collection by its name. You reference a collection element with the syntax collection(index).

PL/SQL has three kinds of collection types:

  • Associative array (formerly called PL/SQL table or index-by table)

  • Variable-size array (varray)

  • Nested table

An associative array can be indexed by either a string type or PLS_INTEGER. Varrays and nested tables are indexed by integers.

You can create a collection variable in either of these ways:

  • Define a collection type and then declare a variable of that type.

  • Use %TYPE to declare a collection variable of the same type as a previously declared collection variable.

Note:

This topic applies to collection types that you define inside a PL/SQL block or package, which differ from standalone collection types that you create with the "CREATE TYPE Statement".

In a PL/SQL block or package, you can define all three collection types. With the CREATE TYPE statement, you can create nested table types and VARRAY types, but not associative array types.

Topics

Syntax

collection_type_definition ::=

Description of collection_type_definition.eps follows
Description of the illustration collection_type_definition.eps

assoc_array_type_def ::=

Description of assoc_array_type_def.eps follows
Description of the illustration assoc_array_type_def.eps

See:

varray_type_def ::=

Description of varray_type_def.eps follows
Description of the illustration varray_type_def.eps

See "datatype ::=".

nested_table_type_def ::=

Description of nested_table_type_def.eps follows
Description of the illustration nested_table_type_def.eps

datatype ::=

Description of datatype.eps follows
Description of the illustration datatype.eps

See:

collection_variable_dec ::=

Description of collection_variable_dec.eps follows
Description of the illustration collection_variable_dec.eps

See "collection_constructor ::=".

Semantics

collection_type_definition

type

Name of the collection type that you are defining.

assoc_array_type_def

Type definition for an associative array.

Restriction on assoc_array_type_def

Can appear only in the declarative part of a block, subprogram, package specification, or package body.

nested_table_type_def

Type definition for a nested table.

varray_type_def

Type definition for a variable-size array.

assoc_array_type_def

datatype

Data type of the elements of the associative array. datatype can be any PL/SQL data type except REF CURSOR.

NOT NULL

Imposes the NOT NULL constraint on every element of the associative array. For information about this constraint, see "NOT NULL Constraint".

{ PLS_INTEGER | BINARY_INTEGER }

Specifies that the data type of the indexes of the associative array is PLS_INTEGER.

{ VARCHAR2 | VARCHAR | STRING } (v_size)

Specifies that the data type of the indexes of the associative array is VARCHAR2 (or its subtype VARCHAR or STRING) with length v_size.

You can populate an element of the associative array with a value of any type that can be converted to VARCHAR2 with the TO_CHAR function (described in Oracle Database SQL Language Reference).

Caution:

Associative arrays indexed by strings can be affected by National Language Support (NLS) parameters. For more information, see "NLS Parameter Values Affect Associative Arrays Indexed by String".

LONG

Specifies that the data type of the indexes of the associative array is LONG, which is equivalent to VARCHAR2(32760).

Note:

Oracle supports LONG only for backward compatibility with existing applications. For new applications, use VARCHAR2(32760).

type_attribute, rowtype_attribute

Specifies that the data type of the indexes of the associative array is a data type specified with either %ROWTYPE or %TYPE. This data type must represent either PLS_INTEGERBINARY_INTEGER, or VARCHAR2(v_size).

varray_type_def

size_limit

Maximum number of elements that the varray can have. size_limit must be an integer literal in the range from 1 through 2147483647.

datatype

Data type of the varray element. datatype can be any PL/SQL data type except REF CURSOR.

NOT NULL

Imposes the NOT NULL constraint on every element of the varray. For information about this constraint, see "NOT NULL Constraint".

nested_table_type_def

datatype

Data type of the elements of the nested table. datatype can be any PL/SQL data type except REF CURSOR or NCLOB.

If datatype is a scalar type, then the nested table has a single column of that type, called COLUMN_VALUE.

If datatype is an ADT, then the columns of the nested table match the name and attributes of the ADT.

NOT NULL

Imposes the NOT NULL constraint on every element of the nested table. For information about this constraint, see "NOT NULL Constraint".

datatype

collection_type

Name of a user-defined varray or nested table type (not the name of an associative array type).

object_type

Instance of a user-defined type.

record_type

Name of a user-defined type that was defined with the data type specifier RECORD.

ref_cursor_type

Name of a user-defined type that was defined with the data type specifier REF CURSOR.

scalar_datatype

Name of a scalar data type, including any qualifiers for size, precision, and character or byte semantics.

collection_variable_dec

new_collection_var

Name of the collection variable that you are declaring.

assoc_array_type

Name of a previously defined associative array type; the data type of new_collection_var.

varray_type

Name of a previously defined VARRAY type; the data type of new_collection_var.

nested_table_type

Name of a previously defined nested table type; the data type of new_collection_var.

collection_constructor

Collection constructor for the data type of new_collection_var, which provides the initial value of new_collection_var.

collection_var_1

Name of a previously declared collection variable of the same data type as new_collection_var, which provides the initial value of new_collection_var.

Note:

collection_var_1 and new_collection_var must have the same data type, not only elements of the same type.

collection_var_2

Name of a previously declared collection variable.

%TYPE

See "%TYPE Attribute".

Examples

  • Example 5-1, "Associative Array Indexed by String"

  • Example 5-2, "Function Returns Associative Array Indexed by PLS_INTEGER"

  • Example 5-4, "Varray (Variable-Size Array)"

  • Example 5-5, "Nested Table of Local Type"

  • Example 5-11, "Two-Dimensional Varray (Varray of Varrays)"

  • Example 5-12, "Nested Tables of Nested Tables and Varrays of Integers"

Related Topics

In this chapter:

In other chapters:

 

 

 

 

 

 

 

 

 

 当前位置:首页 > Oracle > 【Oracle】record、varray、table和%type、%rowtype的使用详解 大屏阅读

【Oracle】record、varray、table和%type、%rowtype的使用详解

 2年前 (2016-08-18)  分类:Oracle  阅读(716)  评论(0)

1  说明

1.1  RECORD

定义记录数据类型。它类似于c语言中的结构数据类型(structure),pl/sql提供了将几个相关的、分离的、基本数据类型的变量组成一个整体的方法,即record复合数据类型。在使用记录数据类型变量时,需要在声明部分先定义记录的组成、记录的变量,然后在执行部分引用该记录变量本身或其中的成员。

定义记录数据类型的语法如下:

SQL

type record_name is record(
    v1  data_type1 [not null][:=default_value],
    v2  data_type2 [not null][:=default_value],
    vn  data_typen [not null][:=default_value]
);

 

1.2  VARRAY

数组是具有相同数据类型的一组成员的集合。每个成员都有一个唯一的下标,它取决于成员在数组中的位置。在pl/sql中,数组数据类型是varray(variable array,即可变数组)。

定义varray数据类型的语法如下:

SQL

type varray_nameis varray(size) of element_type [not null];

 

其中,varray_name是varray数据类型的名称,size是正整数,表示可以容纳的成员的最大数量,每个成员的数据类型是element_typeo默认时,成员可以取空值,否则需要使用not null加以限制。

1.3  TABLE

定义记录表(或索引表)数据类型。它与记录类型相似,但它是对记录类型的扩展。它可以处理多行记录,类似于c语言中的二维数组,使得可以在pl/sql中模仿数据库中的表。

定义记录表类型的语法如下:

SQL

  type table name is table of element_type [not null]
  index by [binary_integer|pls_integer|varray2];

 

关键字index by表示创建一个主键索引,以便引用记录表变量中的特定行。

binary_integer的说明

如语句:type numbers  is table of number index by binary_integer;其作用是,加了”index bybinary_integer ”后,numbers类型的下标就是自增长,numbers类型在插入元素时,不需要初始化,不需要每次extend增加一个空间。

而如果没有这句话“indexby binary_integer”,那就得要显示对初始化,且每插入一个元素到numbers类型的table中时,都需要先extend。

举例

2.1  创建表结构以及数据准备

SQL

--组织机构结构表    
create table sf_org(    
    org_id int not null, --组织机构主键id    
    org_name varchar2(50),--组织机构名称    
    parent_id int--组织机构的父级    
)    
    
--一级组织机构    
insert into sf_org(org_id, org_name, parent_id) values(1, '一级部门1',0);    
    
--二级部门    
    
insert into sf_org(org_id, org_name, parent_id) values(2, '二级部门2',1);    
insert into sf_org(org_id, org_name, parent_id) values(3, '二级部门3',1);    
insert into sf_org(org_id, org_name, parent_id) values(4, '二级部门4',1);

 

2.2  RECORD的使用举例

先定义一个只与sf_org表中某几个列的数据类型相同的记录数据类型type_org_record,然后声明一个该数据类型的记录变量v_org_record,最后用替换变量&org_id接受输入的雇员编码,查询并显示该雇员的这几列中的信息。注意,在使用record数据类型的变量时要用“.”运算符指定记录变量名限定词。

一个记录类型的变量只能保存从数据库中查询出的一行记录,如果查询出了多行记录,就会出现错误。

SQL

--  RECORD的使用举例  
declare     
  type type_org_record is record(    
     v_name    sf_org.org_name%type,  
     v_parent  sf_org.parent_id%type);  
       
     v_record  type_org_record;  
begin    
  select org_name, parent_id into v_record from sf_org so    
  where so.org_id = &org_id;    
        dbms_output.put_line('部门名称:' || v_record.v_name);    
        dbms_output.put_line('上级部门编码:' || to_char(v_record.v_parent));    
end;

 

执行时会弹出一个输入框,如下图,执行完毕以后可以在输出中查看效果

record、varray、table和%type、%rowtype的使用详解

record、varray、table和%type、%rowtype的使用详解

2.3   VARRAY的使用举例

先定义一个能保存5个varchar2(25)数据类型的成员的varray数据类型org_varray_type,然后声明一个该数据类型的varray变量v_org_varray,最后用与org_varray_type数据类型同名的构造函数语法给v_org_varray变量赋予初值并显示赋值结果。

注意,在引用数组中的成员时.需要在一对括号中使用顺序下标,下标从1开始而不是从0开始。

SQL

---  VARRAY的使用举例  
declare     
  type org_varray_type is varray(5) of varchar2(25);    
  v_arr_set org_varray_type;    
begin    
  v_arr_set := org_varray_type('1','2','3','4','5');    
  dbms_output.put_line('输出1:' || v_arr_set(1) || '、'|| v_arr_set(2) || '、'|| v_arr_set(3) || '、'|| v_arr_set(4));    
  dbms_output.put_line('输出2:' || v_arr_set(5));    
  v_arr_set(5) := '5001';    
  dbms_output.put_line('输出3:' || v_arr_set(5));    
end;

 

record、varray、table和%type、%rowtype的使用详解

2.4  TABLE使用举例

2.4.1  存储单列多行

这个和varray类似。但是赋值方式稍微有点不同,不能使用同名的构造函数进行赋值。具体的如下:

SQL

-- 存储单列多行  
declare     
  type org_table_type is table of varchar2(25)    
  index by binary_integer;    
  v_org_table org_table_type;    
begin    
  v_org_table(1) := '1';    
  v_org_table(2) := '2';    
  v_org_table(3) := '3';    
  v_org_table(4) := '4';    
  v_org_table(5) := '5';    
  dbms_output.put_line('输出1:' || v_org_table(1) || '、'|| v_org_table(2) || '、'|| v_org_table(3) || '、'|| v_org_table(4)||'、'|| v_org_table(5));    
end;

 

2.4.2   存储多列多行和ROWTYPE结合使用

采用bulkcollect可以将查询结果一次性地加载到collections中。而不是通过cursor一条一条地处理。

SQL

-- 存储多列多行和rowtype结合使用  
declare     
   type t_type is table of sf_org%rowtype;    
   v_type  t_type;    
 begin    
    select org_id, org_name, parent_id bulk collect into v_type from sf_org where sf_org.org_id <= 3;    
    
    for v_index in v_type.first .. v_type.last loop    
        dbms_output.put_line(v_type(v_index).org_id ||' '|| v_type(v_index).org_name ||' '|| v_type(v_index).parent_id );    
    end loop;    
 end;

 

record、varray、table和%type、%rowtype的使用详解

2.4.3  存储多列多行和RECORD结合使用

采用bulkcollect可以将查询结果一次性地加载到collections中。而不是通过cursor一条一条地处理。

SQL

-- 存储多列多行和RECORD结合使用  
declare     
   type test_emp is record    
   (    
    c1  sf_org.org_name%type,    
    c2  sf_org.parent_id%type    
   );       
   type t_type is table of test_emp;    
   v_type  t_type;    
 begin    
    select org_name, parent_id bulk collect into v_type from sf_org where sf_org.org_id <= 3;    
    
    for v_index in v_type.first .. v_type.last loop    
        dbms_output.put_line(v_type(v_index).c1 || ' ' || v_type(v_index).c2);    
    end loop;    
 end;

 

record、varray、table和%type、%rowtype的使用详解

问题

varry和table集合不能直接对其进行查询。只能对其进行遍历。

其他

在我的Oracle 创建 split 和 splitstr 函数文章中,有包含table的查询示例图:http://www.ibloger.net/article/232.html

%TYPE说明

为了使一个变量的数据类型与另一个已经定义了的变量(尤其是表的某一列)的数据类型相一致,Oracle提供了%TYPE定义方式。当被参照的那个变量的数据类型改变了之后,这个新定义的变量的数据类型会自动跟随其改变,容易保持一致,也不用修改PL/SQL程序了。当不能确切地知道被参照的那个变量的数据类型时,就只能采用这种方法定义变量的数据类型。

%ROWTYP说明

如果一个表有较多的列,使用%rowtype来定义一个表示表中一行记录的变量,比分别使用%type来定义表示表中各个列的变量要简洁得多,并且不容易遗漏、出错。这样会增加程序的可维护性。

为了使一个变量的数据类型与一个表中记录的各个列的数据类型相对应、一致,oracle提供%rowtype定义方式。当表的某些列的数据类型改变了之后,这个新定义的变量的数据类型会自动跟随其改变,容易保持一致,也不用修改pl/sql程序了。当不能确切地知道被参照的那个表的结构及其数据类型时,就只能采用这种方法定义变量的数据类型。

 

未经允许请勿转载:程序喵 » 【Oracle】record、varray、table和%type、%rowtype的使用详解

点  赞 (0) 打  赏

分享到:

 标签:Oracle

http://www.ibloger.net/article/230.html

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值