Oracle函数重载

Oracle函数重载,oracle,function,exception,Oracle,Function,Exception,我有两张桌子:比尔: Bill_Number (PK/FK), Menu_Item_Number (PK/FK), Discount, Quantity_Sold, Price. 和项目: Item_Number (PK), Item_Name, Current_Price, Production_Cost. 现在,我想创建一个函数,该函数将接收商品编号作为输入,并在折扣后返回完整的SUMQuantity\u Seld*价格和SUMQuantity\u Seld*价格 在我的例外情况中,如果

我有两张桌子:比尔:

Bill_Number (PK/FK),
Menu_Item_Number (PK/FK),
Discount,
Quantity_Sold,
Price.
和项目:

Item_Number (PK),
Item_Name,
Current_Price,
Production_Cost.
现在,我想创建一个函数,该函数将接收商品编号作为输入,并在折扣后返回完整的SUMQuantity\u Seld*价格和SUMQuantity\u Seld*价格

在我的例外情况中,如果Item表中不存在Item编号,我希望处理NO_DATA_FOUND错误。如果商品编号存在但从未售出,我还想返回消息。我使用了第一个案例的NO_数据,但项目编号不存在,所以第二个案例我必须使用哪一个

这是我的代码:

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;
紧接着

Where Bill.Item_Number = V_Item_Number;
可能有用


更准确的方法是检查bill where item的计数*。

我通过修改1篇文章中的一些拼写来完成。谢谢大家。

或选中存在项从账单中选择1,其中项目..:下次只需编辑您的原始答案,而不是删除它并发布第二个答案。有没有办法通过例外部分处理这两件事?我正在考虑使用该语句同时选择Item.Item_编号和Bill.Item_编号,并在未找到数据的情况下处理它们,或者像您所说的,在这些表的1上选择countItem_编号。您认为在账单表或项目表上使用select count更好吗?您可以扩展您的第一个选择:从项目编号=项目编号=项目编号的项目中选择项目编号为V\U项目编号,然后退出从账单中选择1。项目编号=项目编号;不确定,顺便说一句,计数永远不应该为空。您的WHEN OTHERS子句是错误的做法。您禁止显示真实消息中的所有有用信息,而是返回一条通用的无用消息。应用程序管理员应该怎么做?只是一个练习的消息,我以后要修改它修改后的代码做了什么你不希望它做的事情?你希望它做什么,它做什么?我不清楚你的问题是什么。事实上,你并没有在你的查询中加入bill\u项和menu\u项,这显然是有问题的,但由于我现在不知道你想解决什么问题,我不确定这是否是错误的根源。事实上,我必须在我的函数中检查两种情况:\菜单中不存在项目号\项目表中不存在项目号\账单中不存在项目号\项目表中该项目从未销售过\如果输入不在这两种情况下:选择合计账单。价格*账单。销售数量\账单。价格*账单。销售数量*账单。折扣/100和合计账单。销售数量*菜单项。当前价格和我在SELECT语句中有错误
Where Bill.Item_Number = V_Item_Number;