2016-12-31 71 views
-1

對於令人困惑的標題感到抱歉,但無法找到另一種方式提問。如何根據其他表中的字段計算插入記錄時的字段(在過程中)

假設我想寫一個程序add_salesline。我輸入所有帶參數的字段,但subtotal除外。小計(只是銷售線的價格)需要根據其他表中的字段進行計算,如products表中的productprice,pricereduction表中的promotion等(基於屬性)。

我該怎麼做?我一直試圖解決這個問題一個良好的一週,現在,它只是不工作...

回答

0

大概是傳遞到程序或其他什麼參數之一。所以你用它來選擇products.productprice,promotion.pricereduction和你需要執行的計算。

編寫存儲過程的目的是將多個調用關聯到單個程序單元。所以add_salesline()可能看起來像這樣(很多需要注意的地方,因爲你的問題是很輕的詳細信息):

create or replace procedure add_salesline(
    p_orderno in salesline.orderno%type 
    , p_salesqty in salesline.salesqty%type 
    , p_productid in products.productid%type 
) 
is 
    new_rec salesline%rowtype; 
begin 
    new_rec.orderno := p_orderno; 
    new_rec.salesqty := p_salesqty; 
    new_rec.productid := p_productid; 

    select p_salesqty * (p.productprice * nvl(pp.pricereduction, 1)) 
    into new_rec.subtotal 
    from products p 
     left outer join promotion pp 
     on pp.productid = p.productid 
    where p.productid = p_productid 
    ; 

    insert into salesline 
    value new_rec; 

end; 

此代碼假定pricereduction是速率。如果該值爲絕對摺扣,則公式將與(p.productprice - nvl(pp.pricereduction, 0))不同。或者如果它是重置價格:coalesce(pp.pricereduction, p.productprice)

+0

謝謝,這確實有幫助。對於不清楚的問題,我感到抱歉,我寫的時候真的很累。 – Ortix

相關問題