在 oracle 中更新 table 过程

Update table procedure in oracle

我有 3 个table,它们是

项目 : Item_Number PK, Item_Name, Current_Price,Production_cost.

成分:Ingredient_Number PK、Ingredient_Name、Ingredient_Cost

Item_Ingredient: Item_Number PK/FK, Ingredient_Number PK/FK, Quantity_Needed

1 件商品由 3 种或 4 种不同的原料制成。 每个项目的 Production_Cost 是通过所有项目的总和计算的 (ingredient_Cost * Quantity_Needed)。 如果生产成本 < 总和 (ingredient_Cost * Quantity_Needed),我正在尝试使用光标创建更新所有项目的 Production_cost 的过程。但我没有知道如何在我的程序中计算它。 你们能帮我修一下吗?这是我的代码的第一部分:

CREATE OR REPLACE PROCEDURE PR_Check_Cost
AUTHID CURRENT USER 
IS
V_Production_Cost item.Production_cost%Type;
V_New_Production_Cost number (6,2);
V_Item_Number number (5,0);
Cursor C_Cost IS
 select ii.item_number, SUM(in.ingredient_cost * ii.quantity_Needed)
 from item.ingredient ii, ingredient in
where ii.ingredient_number = in_ingredient_number
group by ii.item_number
Begin
Open C_Cost;
Fetch C_Cost into V_Item_Number, V_New_Production_Cost;
 While C_Product_Cost%FOUND LOOP
select Production_Cost
into V_Production_Cost
From Item
Where Item_Number = V_Item_Number;

If V_Production_Cost < V_New_Production_Cost THEN
 UPDATE Item
 Set Production_Cost = V_New_Production_Cost
 Where Item_Number = V_Item_Number;
End If;

Fetch C_Cost into V_Item_Number, V_New_Production_Cost;
 End loop;
Close C_Cost;
End PR_Check_cost;/
SHOW ERRORS;

你可以用一个 UPDATE 语句完成它,像这样:

UPDATE item     
SET production_cost = (SELECT SUM(t2.ingredient_cost * t1.quantity_needed)
                       FROM item_ingredient t1 JOIN ingredient t2 ON t1.ingredient_number = t2.ingredient_number
                       WHERE t1.item_number = item.item_number)
WHERE production_cost < (SELECT SUM(t2.ingredient_cost * t1.quantity_needed)
                         FROM item_ingredient t1 JOIN ingredient t2 ON     t1.ingredient_number = t2.ingredient_number
                         WHERE t1.item_number = item.item_number);

不知道你是单纯想搞定它还是想了解游标和过程,那样的话,你可以使用这样的过程(但这样效率很低):

CREATE OR REPLACE PROCEDURE pr_check_cost AUTHID CURRENT USER IS  
  v_production_cost item.production_cost%TYPE;

  CURSOR c_cost IS
    SELECT it.item_number, SUM(in.ingredient_cost * it.quantity_needed) production_cost
    FROM item_ingredient it, ingredient in
    WHERE it.ingredient_number = in.ingredient_number
    GROUP BY it.item_number;
BEGIN

  FOR r IN c_cost LOOP
    SELECT production_cost
    INTO v_production_cost
    FROM item
    WHERE item_number = r.item_number;

    IF v_production_cost < r.production_cost THEN
      UPDATE item
      SET production_cost = r.production_cost
      WHERE item_number = r.item_number;
    END IF;
  END LOOP;

  COMMIT;
END pr_check_cost;

请注意,我还没有编译代码,它可能有一些错误。

我认为你的代码对于你想要实现的目标来说有点太复杂了。 IMO 更好、更简单的方法是 运行 整个项目的游标 table 然后 运行 每个项目的查询,将其结果与当前生产成本进行比较 -

select sum(total_ing_cost)
into calc_cost
from (
select (d.ingredient_cost * q.quantity_needed) as total_ing_cost
from item i, ingredient d, item_ingredient q
where i.item_number = q.item_number
and d.ingredient_number = q.ingredient_number
and item_number = current_item_in_cursor
)

我还没有在这个站上安装 Oracle,所以查询的语法可能不完全正确,但想法是存在的,欢迎您使用它。