2015-10-21 85 views
0

我有一個項目的嵌套表,如果可能的話,我想使用sql語句像sum。像...嵌套表的總和

create type SumMe as object 
(
    my_num number 
); 
/
create type SumThese as table of SumMe; 
/
declare 
    local_summation SumThese; 
    l_totalSum number; 
begin 
    --populate local_summation 
    select sum(my_num) into l_totalSum from local_summation; 
end; 

這可能嗎?嵌套表不是必需的,但我需要將數組類型存儲到數據庫表中。

回答

3

可以使用table collection expression對待你的表類型爲查詢的表:

select sum(my_num) into l_totalSum from table(local_summation); 

快速演示:

set serveroutput on; 
declare 
    local_summation SumThese; 
    l_totalSum number; 
begin 
    --populate local_summation 
    local_summation := SumThese(); 
    local_summation.extend(2); 
    local_summation(1) := SumMe(22); 
    local_summation(2) := SumMe(20); 

    select sum(my_num) into l_totalSum from table(local_summation); 
    dbms_output.put_line('Total: ' || l_totalSum); 
end; 
/

PL/SQL procedure successfully completed. 
Total: 42