2016-09-12 10 views
0

私はsale_numberによってsales_headに付加されたすべてのsale_linesの値を合計する関数を持っています。問題はPL/SQLプロシージャ/ファンクションの数値のリストを作成して返す

create or replace function sale_line_accounts 
     return number is 
     total_value_all number(38, 2); 
     value_sales number(12, 2); 
     last_month date; 
     this_month date; 
     sumPrice number(12,2); 
-- type array_s is varray(200) of varchar2(30); 
-- sums array_s := array_s(); 
begin 
-------------------------------------- 
last_month := to_date('01/08/16'); 
this_month := to_date('03/08/16'); 
--------------------------------------  
    FOR headers IN 
    (
    select sale_num, sale_date, status 
    from sale_head 
    where status = 'S' AND sale_date BETWEEN last_month AND this_month 
    ) LOOP 
------------------------------------------- 
--sums.extend();--extend array 
sumPrice:= 0; 
     FOR lines IN 
     (
     select sale_num, quantity, actual_price 
     from sale_line 
     where sale_num = headers.sale_num 
    )LOOP 

     sumPrice := sumPrice + (lines.quantity * lines.actual_price); 
     dbms_output.put_line(sumPrice); 

--  sums(sumPrice) := lines.sale_num; 
     END LOOP; 

------------------------------------------ 
    END LOOP; 
------------------------------------------- 
    return sumPrice; 
END; 
/

コメントしたコードは、コレクションか何かを作成することを意図していた...私はそれが特定の時間枠内のすべてのsale_headの合計を返すために必要なのに対し、機能のみ、1点の合計を返します、ありますか?助けてください!!

+1

戻したいんコレクションのどのような?ネストしたテーブル、連想配列、またはVARRAY?あるいは 'sys_refcursor'を返すのですか?あなたはそのコレクションをどのように見せたいですか?あなたはこの関数を呼び出すときに何をするつもりですか?私はあなたが 'sys_refcursor'を返すか、' sale_num'と 'sumPrice'を持つオブジェクトを返すパイプラインテーブル関数を作成したいと思います。しかし、それは私が推測していることです - 可能な代替署名がたくさんあります。 –

+1

あなたはこのようなものを試しましたか?[SP Oracleから複数の行を返す方法](http://stackoverflow.com/questions/101033/how-to-return-multiple-rows-from-theored-procedure-oracle- PL-SQL)? – Angel

+4

'to_date('01/08/16 ')'は私を率直に恐れています。 –

答えて

0

あなたはおそらく、単一のSQLクエリを使用して、あなたの答えを得ることができます。

select headers.sale_num 
     ,sum(lines.quantity * lines.actual_price) price 
    from sale_head headers 
    join sale_line lines 
    on lines.sale_num = headers.sale_num 
where headers.status = 'S' 
    and headers.sale_date between to_date('01/08/2016', 'DD/MM/YYYY') and to_date('03/08/2016', 'DD/MM/YYYY') 
group by headers.sale_num 
関連する問題