2012-04-20 134 views
4

我對如何打印定義類型的對象的所有值有疑問。我有一個PROJECTT類型的對象表SP。當我在SP中查詢其對象的行時,它將返回對象的類型(PROJECTT),但不返回對象的內容。 PROJECTT類型有5個varchar列,這是我想要返回的。從一列嵌套表中選擇SQL

下面是我的SQL我的表,然後使用Java SAX解析器和XML文件類型

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) 

,我填的活性成分的SP柱在POBJECTS表中的行引用。

我想用select語句打印這些行。我不太清楚如何讓SP的目標的價值,所以我就開始喜歡的東西:

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

請發佈您的表和類型的CREATE語句以及您的查詢的SQL。 – 2012-04-20 16:56:08

+0

+1用於補充問題@BobJarvis評論 – Matthew 2012-04-20 19:27:46

+0

更新了更多信息! :) – Teknos 2012-04-20 20:48:34

回答

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;的末尾添加nested table sp store as sp_tab;

+0

非常好!我的錯誤,我錯誤地離開了這條線。你的解決方案很棒。謝謝! – Teknos 2012-04-21 04:22:05