【问题标题】:create a trigger to derive column values from another table创建触发器以从另一个表中派生列值
【发布时间】:2023-03-24 05:32:01
【问题描述】:

我有两张桌子

Items

ITEM_CODE   VARCHAR2(20)    
ITEM_NAME   VARCHAR2(20)
PRICE_TON   NUMBER(38,5)    
PRICE_REAM  NUMBER(38,5)    
PRICE_SHEET NUMBER(38,5)

Orderitems

ORDER_ITEMS_CODE    VARCHAR2(20)    
ORDER_CODE          VARCHAR2(20)    
ITEM_CODE_ORDERS    VARCHAR2(20)
ORDER_QUANTITY          NUMBER(4,0) 
ORDER_UNIT          VARCHAR2(5)
UNIT_PRICE          NUMBER(38,5)    

我想创建一个触发器来根据 order_unit 计算 unit_price 我试过这个触发器,但它不起作用

 create or replace TRIGGER "Orderitems_T1"
    BEFORE
    insert or update on orderitems
    for each row
    begin   
    declare
    order_unit                        orderitems.order_unit%type;
    unit_price                         orderitems.unit_price%type;
    price_sheet                      Items.price_sheet%type;
    price_ream                       Items.price_ream%type;
    price_ton                           Items.price_ton%type;
    item_code                         Items.item_code%type;
    item_code_orders         orderitems.item_code_orders%type;
    when item_code_orders = item_code then
    begin
    case
    when order_unit ='sheet' then  unit_price :=  price_sheet;
    when order_unit = 'ton'  then  unit_price := price_ton ;
    when order_unit = 'ream'  then  unit_price := price_ream ;
    else unit_price  := 0;
    end case;
    end;
    end;

我收到此错误

PLS-00103:在预期以下情况之一时遇到符号“WHEN”:begin function pragma procedure subtype type current cursor delete exists prior

PLS-00103:遇到符号“;”预期以下情况之一时: case 符号“case”被替换为“;”继续。

【问题讨论】:

  • 您指定的 WHEN 子句位于错误的位置,但更重要的是,您正在命名 ORDERITEMS 表中存在的一个字段 (ITEM_CODE_ORDERS) 和存在的另一个字段 (ITEM_CODE)在项目表中。 WHEN 子句中命名的所有字段都必须存在于触发器操作的表中(在本例中为 ORDERITEMS 表)。请编辑您的问题并详细说明您要完成的工作。谢谢。
  • 我想计算插入到表 ORDERITEMS 中的每个项目的 unit_price,如果 (order_unit) 等于 'ton' 然后 (unit_price) 等于 (Price_ton)当 ITEMS 中的 (item_code) 等于 ORDERITEMS 中的 (Item_code_orders) 时的 ITEMS 表

标签: plsql triggers oracle11g


【解决方案1】:

如前所述,您不能像最初尝试的那样使用 WHEN 子句,因为您不能在 WHEN 子句中使用来自多个表的字段。看起来您需要从 ITEM_CODE 与 ORDER_ITEMS 行上的 ORDER_ITEMS_CODE 匹配的 ITEM 获取行,然后将相应字段从 ITEM_CODE 行复制到 ORDER_ITEMS 行。

试试这个:

create or replace TRIGGER Orderitems_T1
  BEFORE insert or update on orderitems
  for each row
declare
  rowItems  ITEMS%ROWTYPE;
begin
  SELECT *
    INTO rowItems
    FROM ITEMS i
    WHERE i.ITEM_CODE = :NEW.ORDER_ITEMS_CODE;

  case
    when :NEW.order_unit = 'sheet' then
      :NEW.unit_price := rowItems.price_sheet;
    when :NEW.order_unit = 'ton' then
      :NEW.unit_price := rowItems.price_ton ;
    when :NEW.order_unit = 'ream' then
      :NEW.unit_price := rowItems.price_ream ;
    else
      :NEW.unit_price := 0;
  end case;
EXCEPTION
  WHEN NO_DATA_FOUND THEN
    DBMS_OUTPUT.PUT_LINE('No row found in ITEMS for ' ||
                         'ORDERITEMS.ORDER_ITEMS_CODE=''' ||
                         :NEW.ORDER_ITEMS_CODE || '''');
    RAISE;  -- re-raise the error if an invalid item code is found
end Orderitems_T1;

分享和享受。

【讨论】:

  • 我试过这个,但我得到了这个错误 [PLS-00103: 在预期以下情况之一时遇到符号“WHEN”:( begin case declare exit for goto if loop mod null pragma raise return select update while with
  • 我以为 ORDER_UNIT 字段在 ITEMS 表上。再试一次。
  • 上面的代码对我来说编译没有错误。我不确定我能告诉你什么......
  • 我总是遇到同样的错误,不知道为什么!!但感谢您的帮助
【解决方案2】:

有一些语法错误。
你能试试运行下面的代码吗

create or replace TRIGGER "Orderitems_T1"
BEFORE
insert or update on orderitems
for each row
when (item_code_orders = item_code )
--begin   --remove this begin
AS
order_unit                        orderitems.order_unit%type;
unit_price                         orderitems.unit_price%type;
price_sheet                      Items.price_sheet%type;
price_ream                       Items.price_ream%type;
price_ton                           Items.price_ton%type;
item_code                         Items.item_code%type;
item_code_orders         orderitems.item_code_orders%type;

begin
case
when order_unit ='sheet' then  unit_price :=  price_sheet;
when order_unit = 'ton'  then  unit_price := price_ton ;
when order_unit = 'ream'  then  unit_price := price_ream ;
else order_unit := 0;
end case;
end;

【讨论】:

  • 现在我得到 ORA-04076: invalid NEW or OLD specification!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
  • 2013-12-31
  • 1970-01-01
相关资源
最近更新 更多