4

我有一个关于如何打印定义类型对象的所有值的问题。我有一个 PROJECTT 类型对象的表 SP。当我在 SP 中查询其对象的一行时,它返回对象的类型 (PROJECTT) 但不返回对象的内容。PROJECTT 类型有 5 个 varchar 列,这是我想要返回的。

下面是我的表和类型的 SQL

set echo on
set serveroutput on

drop table ACTIVES;
drop table POBJECT;
drop type ACTIVEST;
drop type POBJECTT;


create type POBJECTT as
   object (p# varchar(5), pname varchar(20), color varchar(10),
           weight number, city varchar(10));

create table POBJECT of POBJECTT;
   insert into POBJECT(p#, pname, color, weight, city) values
    ('P1', 'Nut', 'Red', 12, 'London');
   insert into POBJECT(p#, pname, color, weight, city) values
   ('P2', 'Bolt', 'Green', 17, 'Paris');
   insert into POBJECT(p#, pname, color, weight, city) values
   ('P3', 'Screw', 'Blue', 17, 'Rome');
   insert into POBJECT(p#, pname, color, weight, city) values
   ('P4', 'Screw', 'Red', 14, 'London');
   insert into POBJECT(p#, pname, color, weight, city) values
   ('P5', 'Cam', 'Blue', 12, 'Paris');
   insert into POBJECT(p#, pname, color, weight, city) values
   ('P6', 'Cog', 'Red', 19, 'London');

  create type ACTIVEST as table of ref POBJECTT;
   /

  create table ACTIVES(s# varchar(5), sname varchar(20), status number, 
                     city varchar(10), sp ACTIVEST)

然后使用Java SAX 解析器和XML 文件,我用对POBJECTS 表中行的引用填充ACTIVES 的sp 列。

我正在尝试使用 select 语句打印这些行。我不太确定如何在 SP 中获取对象的值,所以我从以下内容开始:

select * from (select sp from actives where S# = 'S1')
4

1 回答 1

1
select
    deref(column_value).p# p#
    ,deref(column_value).pname pname
    ,deref(column_value).color color
    ,deref(column_value).weight weight
    ,deref(column_value).city city
from actives cross join table(sp)
where s# = 'S1';

P#    PNAME                COLOR          WEIGHT CITY
----- -------------------- ---------- ---------- ----------
P1    Nut                  Red                12 London
P2    Bolt                 Green              17 Paris

SQL>

假设您之前运行过这样的插入:

insert into actives
select 'S1', 'sname', 0, 'city'
    ,activest(
        (select ref(p) from pobject p where p# = 'P1')
        ,(select ref(p) from pobject p where p# = 'P2')
    )
from dual;

此外,您的 ACTIVES DDL 对我不起作用。我不得不添加nested table sp store as sp_tab;到它的末尾。

于 2012-04-21T03:46:05.717 回答