2013-02-15 29 views
0

我有一個表,其中包含oracle COLOR_HISTOGRAM列,它包含值。它是一個帶有colorlist和colorfrequencielist的對象類型。 如果我想將SI_COLORS插入表中,我該如何達到這些屬性?如何達到oracle對象的屬性?

我declated可能包含這些值

--CREATE OR REPLACE TYPE colorsList AS VARRAY(100) OF SI_Color; 
--CREATE OR REPLACE TYPE colorFrequenciesList AS VARRAY(100) OF DOUBLE PRECISION; 


ORDSYS.SI_COLORHISTOGRAM(
ORDSYS.COLORSLIST(
ORDSYS.SI_COLOR(195,157,133), 
ORDSYS.SI_COLOR(226,223,215), 
ORDSYS.SI_COLOR(191,154,131), 
ORDSYS.SI_COLOR(139,94,61), 
ORDSYS.SI_COLOR(221,40,43), 
ORDSYS.SI_COLOR(85,47,30), 
ORDSYS.SI_COLOR(237,45,51), 
ORDSYS.SI_COLOR(190,60,47), 
ORDSYS.SI_COLOR(66,53,49), 
ORDSYS.SI_COLOR(145,66,63), 
ORDSYS.SI_COLOR(192,115,111), 
ORDSYS.SI_COLOR(64,50,46), 
ORDSYS.SI_COLOR(178,151,123), 
ORDSYS.SI_COLOR(178,153,129), 
ORDSYS.SI_COLOR(170,140,114), 
ORDSYS.SI_COLOR(174,150,124), 
ORDSYS.SI_COLOR(146,118,106), 
ORDSYS.SI_COLOR(189,168,145), 
ORDSYS.SI_COLOR(190,171,148), 
ORDSYS.SI_COLOR(144,71,57), 
ORDSYS.SI_COLOR(86,45,26), 
ORDSYS.SI_COLOR(193,109,80), 
ORDSYS.SI_COLOR(145,138,136), 
ORDSYS.SI_COLOR(130,121,120), 
ORDSYS.SI_COLOR(115,80,72), 
ORDSYS.SI_COLOR(186,50,37), 
ORDSYS.SI_COLOR(193,171,149), 
ORDSYS.SI_COLOR(0,0,0)), 
ORDSYS.COLORFREQUENCIESLIST(99014.1,67706.4,38034.6,28478.2,5075.64,3302.56,2429.49,856.41,638.462,560.256,520.513,476.923,439.744,296.154,247.436,232.051,194.872,138.462,123.077,110.256,82.0513,75.641,62.8205,51.2821,46.1538,43.5897,32.0513,0)) 

任何想法,高度讚賞兩種類型。對於兩個陣列

SQL> create table foo(r integer, b integer, g integer); 

Table created. 

SQL> insert into foo 
    2 select cl.redvalue, cl.bluevalue, cl.greenvalue 
    3 from tbl t, table(t.color_histogram.SI_COLORSLIST) cl; 

28 rows created. 

SQL> select * from foo; 

     R   B   G 
---------- ---------- ---------- 
     195  133  157 
     226  215  223 
     191  131  154 
     139   61   94 
     221   43   40 
...etc.. 

回答

1

你可以參考SI_COLOR值這樣

SQL> create table foo(r integer, b integer, g integer, freq number); 

Table created. 

SQL> insert into foo 
    2 select cl.redvalue, cl.bluevalue, cl.greenvalue, fl.freq 
    3 from (select t.rowid rid, cl.redvalue, cl.bluevalue, cl.greenvalue, rownum r 
    4   from tbl t, table(t.color_histogram.SI_COLORSLIST) cl) cl 
    5   inner join (select t.rowid rid, rownum r, fl.column_value freq 
    6      from tbl t, table(t.color_histogram.si_frequencieslist) fl) fl 
    7     on fl.rid = cl.rid 
    8    and fl.r = cl.r; 

28 rows created. 

SQL> select * from foo; 

     R   B   G  FREQ 
---------- ---------- ---------- ---------- 
     195  133  157   1 
     226  215  223   2 
     191  131  154   3 
     139   61   94   4 
..etc.. 

(因爲他們是我們可以依靠的順序,使ROWNUM是確定可變數組)。

或PL/SQL:

SQL> begin 
    2 for r_row in (select t.color_histogram 
    3     from tbl t) 
    4 loop 
    5  for idx in 1..r_row.color_histogram.si_colorslist.count 
    6  loop 
    7  insert into foo 
    8     (r, g, b, freq) 
    9  values (r_row.color_histogram.si_colorslist(idx).redvalue, 
10    r_row.color_histogram.si_colorslist(idx).greenvalue, 
11    r_row.color_histogram.si_colorslist(idx).bluevalue, 
12    r_row.color_histogram.si_frequencieslist(idx)); 
13  end loop; 
14 end loop; 
15 end; 
16/

PL/SQL procedure successfully completed. 

SQL> select * from foo; 

     R   B   G  FREQ 
---------- ---------- ---------- ---------- 
     195  133  157   1 
     226  215  223   2 
     191  131  154   3 
     139   61   94   4 
     221   43   40   5 
     85   30   47   6 
etc.. 
+0

由於做工精細:)我用PL/SQL複雜了。 – Greg 2013-02-15 14:14:02

+0

我想知道它是否適用於colorFrequenciesList? – Greg 2013-02-15 14:46:14

+0

@Greg當然你也可以提取這些,也就是說你可以在table子句中使用't.color_histogram.COLORFREQUENCIESLIST'。還是你的意思是你想要同時將RGB +相關頻率存儲在同一行? – DazzaL 2013-02-15 14:48:50