oracle-可变数组

1.首先创建一个类型(类似于创建一个普通的Java类),用于来做可变数组的类型
该类型的变量支持Oracle支持的所有类型.
当申明了数组后,数组在该表中作为一列存在

语法: create type typename(类型名称) as object
(
id int,
describe varchar2(50)
....... //类型的元素
);

2.创建可变数组

语法:create type arrayname(数组名称) as varray(50) of typename(类型名称)
说明: varray(50) 指定数组长度

3.创建表

craete table user_table
(
id int,
name varchar2(20),
sex int,
arrays arrayname //数组类型
);


查询有可变数组的表:

例子: select * from table ( select u.arrays from user_table u);

说明: select * from table 其中table不特指标明,表名在后面的子查询中
select u.arrays from user_table u 指在user_table中查询,并且查询可变数组

查询得到ResultSet后,与其余select语句操作方法相同,可使用下标获取列的数据
或使用列名获取数据

插入
insert into user_table(2, 'test arrays', 1,
arrayname
(
typename(1, 'Describe ...'),
typename(2, 'Describe ...')
)
);

说明: 插入时,数组作为一列操作.
其中 arrayname 为数组对象,名称
typename 为数组的类型(类型是JavaBen)
一个arrayname中可以有多个数组.
更新参考Insert操作

删除与普通无数组表无区别


///----------------------------

1、创建类型propvalue
create type propvalue as object (
cid int,
pid int,
prop_name varchar2(30),
vid int,
name varchar2(20),
is_parent int
);
2、创建可变数组PropValues

create type propvalues as varray(50) of propvalue;
3、创建表
create table item_prop
(
pid int,
name varchar2(20),
is_key_prop int,
is_sale_prop int,
is_color_prop int,
parent_pid int,
parent_vid varchar2(20),
prop_values propvalues
);

4、向可变数组插入记录

insert into item_prop
values(1627207, '颜色',0,1,1,0,0,
propvalues(
propvalue(50010538,1627207,'颜色', 3232483, '军绿色',1),
propvalue(50010538,1627207,'颜色', 3232484, '天蓝色',1)
)
);

insert into item_prop

values(20509, '尺码',0,1,0,0,0,
propvalues(
propvalue(50010538,20509,'尺码', 28314, 'S',1),
propvalue(50010538,20509,'尺码', 28315, 'M',1)
)
);
5、查询可变数组

Select E .* From Item_Prop Itm , Table(Prop_Values) E
Where E.Cid=50010538 And Itm.Pid=E.Pid

猜你喜欢

转载自x125858805.iteye.com/blog/1566547