Oracle 中的表变量(数组变量)


表类型变量又称index_by表或则PL/SQL表,和数据表是有区别的,是类似于数组的键和值的两列结构。

其定义语法如下:

type 表类型名 is table of 类型 index by binary_integer;

表变量名 表类型;

类型可以是number 、varchar2、date 等数据类型,index by binary_integer 子句代表以符号整数为索引,

这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”,注意与数组不同的是该索引可以是不连续的,而且与数据库表不同

不能在index-by 表中使用select、insert、update 等sql操作。

下面的程序定义了名为tabletype1和tabletype2的两个一维表类型,相当于一维数组。table1和table2分别是两种表类型变量。

Declare

type tabletype1 is table of varchar2(10) index by binary_integer;

type tabletype2 is table of  scott.testtable.recordnumber%type  index by binary_integer;

table1 tabletype1;

table2 tabletype2;

begin

table1(1):='大学';

table1(2):='大专';

table2(1):=88;

table2(2):=55;

dbms_output.put_line(table1(1)||table2(1));

dbms_output.put_line(table1(2)||table2(2));

end;

定义多维表类型变量 :

Declare

type tabletype1 is table of testtable%rowtype index by binary_integer;

table1 tabletype1;

begin

select * into table1(60)

from scott.testtable

where recordnumber=60;

dbms_output.put_line(table1(60).recordnumber||'  '||table1(60).currentdate);

end;

该程序定义了名为tabletype1的多维表类型,相当于多维数组,table1是多维表类型变量,将数据表tempuser.testtable中recordnumber为60的记录提取出来存放在table1中并显示

在定义好的表类型变量里,可以使用count、delete、first、last、next、exists和prior等属性进行操作,使用方法为“表变量名.属性”,返回的是数字。

相关内容