我对Oracle的程序有疑问,希望你们帮助我。我有这3个表:
成分: Ingredient_Number PK Ingredient_Name, Quantity_On_Hand, 订货点, Current_Cost,
MENU_ITEM
Menu_Item_Number PK
Menu_Item_Name,
目前的价格,
Production_Cost,
Menu_Item_Ingredient
Menu_Item_Number PK / FK,
Ingredient_Number PK / FK ,
Quantity_Needed,
如果Production_Cost<
我尝试创建更新Menu_Item表上的Production_Cost的过程。 Quantity_Needed * Current_cost * 0.75我最终得到了这些代码:
Create or Replace Procedure PR_Check_Cost
(P_Menu_Item_Number Number, P_Ingredient Number)
authid current_user
IS
V_Productiton_Cost Number (6,2);
V_Current_Cost
V_Quantity_Needed Number (5,2);
CURSOR C_Cost
IS
Select Production_Cost
INTO V_Production_Cost
From Menu_Item
Where Menu_Item_Number= P_Menu_Item_Number;
Begin
Open C_Cost;
Select Quantity_Needed
Into V_Quantity_Needed
FROM Menu_Item_Ingredient
WHERE Ingredient_Number = P_Ingredient_Number
AND Menu_Item_Number = P_Menu_Item_Number;
Select Current_Cost
INTO V_CurrentCost
From Ingredient
Where Ingredient_Number = P_Ingredient_Number;
Fetch C_Product_Cost INTO V_Current_Cost, V_Quantity_Needed
While C_Product_Cost%FOUND LOOP
If (V_Quantity_Needed * V_Current_Cost * 0.75) > V_Production_Cost
{
UPDATE Menu_Item
SET Production_Cost = V_Quantity_Needed * V_Current_Cost * 0.75
Where Menu_Item_Number = P_Menu_Item_Number
AND Ingredient-Number = P_Ingredient_Number;
}
End PR_Check_Cost;/
SHOW ERRORS;
无论如何,只要用户更新Current_Cost和Quantity_Needed列而不输入 Menu_Item_Number(PK)和Ingredient_Number(PK)列的输入数据,就会自动更改我的程序以检查和更新Production_Cost
答案 0 :(得分:1)
这是一个解决方案,它只会查询您需要更新的内容,然后使用批量更新它们。它会表现得相当不错。
CREATE OR REPLACE update_pr_costs
AS
DECLARE
menu_item_tab IS TABLE OF ROWTYPE%MENU_ITEM
INDEX BY PLS_INTEGER;
menu_items menu_item_tab;
Begin
Select
mi.Menu_Item_Number,
mi.Menu_Item_Name,
mi.Current_Price,
(mii.Quantity_Needed * ig.Current_Cost * 0.75) Production_Cost
BULK COLLECT INTO menu_items
FROM Menu_Item_Ingredient mii
JOIN Ingredient ig
ON mii.Ingredient_Number = ig.Ingredient_Number
LEFT JOIN Menu_Item mi
ON mi.Ingredient_Number = ig.Ingredient_Number
AND mii.Menu_Item_Number = mi.Menu_Item_Number
WHERE (ig.Current_Cost * mii.Quantity_Needed * 0.75) > mi.Production_Cost;
FORALL i in menu_items.FIRST .. menu_items.LAST
UPDATE Menu_Item
SET Production_Cost = menu_items(i).Production_Cost
where Menu_Item_Number = menu_items(i).Menu_Item_Number
AND Ingredient_Number = menu_items(i).Ingredient_Number;
End PR_Check_Cost;/
SHOW ERRORS;
可用于启动程序的触发器。
CREATE OR REPLACE TRIGGER ingredient_after_update
AFTER UPDATE
ON Ingredient
BEGIN
update_pr_costs;
END;
CREATE OR REPLACE TRIGGER menu_item_ing_after_update
AFTER UPDATE
ON Menu_Item_Ingredient
BEGIN
update_pr_costs;
END;