官方文档:

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

  • Semantics

  • Examples

  • Related Topics

Syntax

collection_type_definition ::=


Description of the illustration collection_type_definition.eps

assoc_array_type_def ::=


Description of the illustration assoc_array_type_def.eps

See:

  • "datatype ::="

  • "rowtype_attribute ::="

  • "type_attribute ::="

varray_type_def ::=


Description of the illustration varray_type_def.eps

See "datatype ::=".

nested_table_type_def ::=


Description of the illustration nested_table_type_def.eps

datatype ::=


Description of the illustration datatype.eps

See:

  • "rowtype_attribute ::="

  • "type_attribute ::="

collection_variable_dec ::=


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:

  • "Collection Method Invocation"

  • "FORALL Statement"

  • "Record Variable Declaration"

  • "%ROWTYPE Attribute"

  • "%TYPE Attribute"

In other chapters:

  • "Collection Topics"

  • "BULK COLLECT Clause"

  • "CREATE TYPE Statement"

当前位置:首页 > 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;

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

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;

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;

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;

问题

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

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

  1. 定义html表格的大小和位置,点晴OA工作流表单设计:表格table及宏控件的HTML、CSS、字体参数教程详解...

     点晴OA工作流表单设计:表格table及宏控件的HTML.CSS.字体参数教程详解 说明:虽然点晴OA内置了强大的富文本编辑器,但是很多时候还是难以实现理想的精确显示效果,为了实现满意的显示效果,网 ...

  2. php array 关联数组,php array_merge关联数组

    我正在尝试将一个项目添加到关联数组的开头.我认为最好的方式是使用array_merge,但我有一些奇怪的后果.我从mysql数据库获取产品的id和Name,并将它作为关联数组返回,就像这样(不是实际的 ...

  3. php array 关联数组,php关联数组的输出

    php 动态关联数组,PHP 反射API,php遍历关联数组,php关联数组的输出 php数组学习数组索引数组关联数组_互联网_IT/计算机_专业资料.php数组 php array php索引数组 ...

  4. oracle数据库标志物,Oracle表的分类以及相关参数的详解

    oracle中有如下几种类型的表: 1.堆组织表(heap organized tables):常用的表类型,以堆的方式管理,当增加数据时,将使用段中第一个适合数据大小的空闲空间:当删除数据时,留下的 ...

  5. 微信小程序php返回数组,微信小程序 数组(增,删,改,查)等操作实例详解...

    微信小程序 数组(增,删,改,查)等操作 最近在做一个小程序的demo.由于不向后台请求数据,所以就涉及到对本地数据的操作,也遇到了一些坑,本文就以数组的增删改查为例,给新手分享一些经验. 首先这是原 ...

  6. 微信小程序js数组初始化_微信小程序 数组(增,删,改,查)等操作实例详解...

    微信小程序 数组(增,删,改,查)等操作 最近在做一个小程序的demo.由于不向后台请求数据,所以就涉及到对本地数据的操作,也遇到了一些坑,本文就以数组的增删改查为例,给新手分享一些经验. 首先这是原 ...

  7. Vue笔记(五)—— Vue render渲染/组件嵌套之iView官网案例改写Table表格组件及Modal弹窗/对话框/模态框组件内容自定义详解

    缺乏耐心的读者请主要关注标红部分! 因部分内容自动转为代码格式,所以代码部分请主要关注注释部分! 1.Table表格组件内容自定义: 官网Table表格组件部分示例代码: columns12: [{t ...

  8. oracle数据库是db还是dbnms,Oracle数据库中各种类型的文件损坏与修复过程详解(2)...

    5.损坏全部联机日志 (1)故障模拟 删除日志文件:rm /u02/oradata/dbnms/*.log 关闭数据库:shutdown immediate; 启动数据库:startup; Datab ...

  9. C语言中二维数组名与数组地址、首行地址、首行首元素地址关系与区别详解(初学者必须掌握)

    C语言作为很多大学理工科都会学习的语言,作为一种编程入门语言. 但是相对于其他高级编程语言来说相对是比较难,尤其是指针,不知道有多少莘莘学子都是因为它,从C语言入门到放弃. 想当年,笔者在大一学习C语 ...

最新文章

  1. XSLT 与 Java集成常见技术关键点
  2. P1160-队列安排【链表】
  3. mfc点击按钮让对话框关闭_WinXP系统开始菜单中关机按钮消失的恢复教程
  4. LATEX调整公式、图片与正文间距离,文字间距离,调整空白大小
  5. centos升级glibc(升级到 2.17版)
  6. 【图像处理】直方图均衡化(附带Matlab及OpenCV3自编程实现代码)
  7. vs2017运行yolov4_YOLOv4 C vs2017 编译
  8. 第3批鸿蒙手机排名,荣耀手机也能升级!第三批鸿蒙手机升级名单大曝光:全球第三稳了...
  9. docker mysql8.0挂载_Docker安装MySQL 8.0.17 并挂载数据及配置文件,修改时区
  10. 【转载】游戏并发编程的讨论 Nodejs并发性讨论 语法糖术语
  11. 大龄程序员找工作,为什么这么难?能力与年龄不匹配
  12. Android 设置无线热点模块隐藏SSID
  13. Altium Designer18学习
  14. Centbrowser网页显示不正常的解决方法
  15. 某音最近很火的挤地铁游戏直播技术:挤地铁直播+源码+软件下载+视频教程下载-亲测可用
  16. python伪原创工具开发_现在有哪些好用的伪原创工具?
  17. 各品牌手机进rec快捷键
  18. 王者荣耀孙策520皮肤特效展示 孙策520皮肤值得买吗
  19. 1024 java学习之路。
  20. 2023年2月东莞/惠州/深圳CPDA数据分析师认证招生简章

热门文章

  1. 常用数据库的基因ID
  2. 数据结构实训-纸牌游戏
  3. 【广东开放大学(广东理工职业学院)主办】第二届计算机图形学、人工智能与数据处理国际学术会议(ICCAID 2022)
  4. 基于具体实验对脑部分区的具体调研
  5. 2022年衡量技术债务的8个主要指标
  6. vsftpd写入延误_技术债务造成的延误成本,第4部分
  7. MQL4自编指标学习6-MQL4中MACD指标的实现
  8. gitub优秀的android开源项目
  9. [ChromeApp]指南!让你的谷歌浏览器好用十倍!
  10. Pycharm全局搜索关键字