2015-10-18 59 views
0

我有两个表:比尔:甲骨文函数重载

Bill_Number (PK/FK), 
Menu_Item_Number (PK/FK), 
Discount, 
Quantity_Sold, 
Price. 

而且项目:

Item_Number (PK), 
Item_Name, 
Current_Price, 
Production_Cost. 

现在我想创建一个将收到ITEM_NUMBER作为输入的功能和返回完整的SUM(Quantity_Sold * Price)和折扣后SUM(Quantity_Sold*Price)

在我的例外情况中,如果Item_Number不存在于Item表中,我想处理NO_DATA_FOUND错误。如果Item_Number存在但我从未售出,我也想要返回消息。我在第一种情况下使用了NO_DATA_FOUND(Item_Number不存在),所以我必须为第二种情况使用哪一种?

这是我的代码:

FUNCTION FN_Check 
    (P_Item_Number NUMBER) 
RETURN Varchar2 
IS 
V_Count Number (5,0); 
V_Item_Number Number (5,0); 
V_Output Varchar2 (500); 
V_TotalDiscount Number (10,2); 
V_CurrentTotal Number (10,2); 
itemHasNotSold Exception; 
Begin 
Select Item_Number 
Into V_Item_Number 
From Menu_Item 
Where Item_Number = P_Item_Number; 

Select NVL(count(Item_Number),0) 
INTO V_Count 
From Bill 
Where Menu_Item_Number = V_Menu_Item_Number; 

If V_Count = 0 THEN 
RAISE itemHasNotSold; 
ELSE 
Select SUM(bi.Selling_Price*bi.Quantity_Sold - bi.Selling_Price*bi.Quantity_Sold*bi.Discount/100), 
SUM(bi.QUANTITY_SOLD *mi.Current_Price) 
Into V_TotalWithDiscount, V_CurrentTotal 
From Bill_Item bi, Menu_Item mi 
Where bi.Item_Number = P_Item_Number and mi.Item_Number= bi.Item_Number; 
V_Output := V_Menu_Item_Number || 'was sold total' || V_TotalWithDiscount || 'and the total should be' || V_CurrentTotal || 'with the current price'; 
END IF; 
    Return V_OutPut; 
EXCEPTION 

WHEN NO_DATA_FOUND THEN 
    RAISE_APPLICATION_ERROR(-20001, 'Menu Item Number does not exist'); 
RETURN V_Output; 

When itemHasNotSold THEN 
RAISE_APPLICATION_ERROR (-20002, 'Item has not sold'); 

WHEN OTHERS THEN 
    RAISE_APPLICATION_ERROR (-20003, 'Data error.Please contact xxx-yyyyyyy for more infomation'); 
End FN_Check; 
+0

你的WHTH OTHERS子句是不好的做法。您将真实消息中的所有有用信息都取消,并返回通用的无用消息。应用程序管理员应该怎么做? – APC

+0

只是一个练习的消息,我稍后会修改它 – GKra

+0

修改后的代码会做什么,您不希望它做什么?你希望它做什么没有做到?我不明白你的问题是什么。事实上,你实际上并没有在你的查询中加入'bill_item'和'menu_item'似乎存在问题,但由于我现在不知道你想要解决什么问题,我不确定这是否是源错误。 –

回答

0
IF V_Total IS NULL THEN 
RETURN 'Nothing sold yet'; 
END IF; 

右后

Where Bill.Item_Number = V_Item_Number; 

可能工作。

更精确的将是从哪里账单项目..

+0

或检查是否存在(从条目中选择1项).. :) – clq

+0

下一次只需编辑您的原始答案,而不是删除它并发布第二个答案。 – APC

+0

有没有办法通过异常部分来处理这两件事情?我考虑使用语句一起选择Item.Item_Number和Bill.Item_Number并使用NO_DATA_FOUND处理它们,或者像您说的那样,在这些表的一个上选择count(Item_Number)。您认为在Bill表或Item表上使用select count()会更好吗? – GKra

0

我得到它修改了一些错别字在#1后进行检查COUNT(*)。谢谢大家。