首页 > 技术文章 > oracle pl/sql中record和%rowtype整理

quzq 2019-12-19 16:01 原文

1. 创建stu表,如下:

  create table stu(s1 number, s2 number);

 

2. 定义多维数组, 能用来接受多条返回数据

  方式一:   type type_name is table of stu%rowtype;           # 基于表中行类型的多维数组

      custom_type type_name;                                    # 定义该多维数组变量

      execute immediate 'select * from stu where s1>:1' bulk collect into custom_type using in 5;

  方式二:   type list_name is record(x number, y number);   # 先定义record, record即一维数组, 

      type type_name is table of list_name;                  # 使用定义的一维数组定义多维数组

      custom_type type_name;               # 定义该多维数组变量

      execute immediate 'select * from stu where s1>:1' bulk collect into custom_type using in 5;

 

3. 定义一维数组, 用来接受一条返回数据

  方式一:   type list_name is record(x number, y number);    # 先定义record, record即一维数组.

      custom_type list_name                  #定义该一维数组变量.

      execute immediate 'select * from stu where s1=:xx' into custom_type using in 1;

  方式二:   custom_type stu%rowtype;                           # 使用表的行类型定义一维数组变量

      execute immediate 'select * from stu where s1=:xx' into custom_type using in 1;

 

4. 以上是演示多行多列和单行多列的返回值接受, 单列多行和单列单行的返回值接受和上面类似, 定义时

    使用table.column%type 或 record(x number)定义行一维的单元素数组后, 再使用同上的定义多维度的

    办法, 即可用来接受单列多维数据或单列单维数据.

推荐阅读