2013-08-12 119 views
0

派生列的值我有两个表创建触发器从另一个表

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:出现符号“何时”在需要下列之一时:开始功能编译程序亚型典型值e当前光标删除先前存在

PLS-00103:遇到符号“;”当期待以下情况之一时:情况符号“case”被替换为“;”接着说。

+0

您指定的WHEN子句位置不正确,但更重要的是您命名ORDERITEMS表中存在的一个字段(ITEM_CODE_ORDERS)和存在于ITEMS表中的另一个字段(ITEM_CODE)。在WHEN子句中命名的所有字段必须存在于触发器操作的表中(在本例中为ORDERITEMS表)。请编辑您的问题并解释更多关于您想要完成的内容。谢谢。 –

+0

我想计算表ORDERITEMS中插入的每个项目的unit_price ,,如果(item_code)时(ITEMS表)中的(order_unit)等于'ton',那么(unit_price)等于(Price_ton)时,此(unit_price)取决于order_unit。在ORDERITEMS – user2648669

回答

1

如前所述,因为你不能在WHEN子句中使用来自多个表中的字段,你不能使用WHEN子句为你最初试图。看起来您需要从ITEMS中获取具有与您的ORDER_ITEMS行上的ORDER_ITEMS_CODE匹配的ITEM_CODE的行,然后将相应的字段从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; 

分享和享受。

+0

我试过这个,但是我收到这个错误 [PLS-00103:在期待以下某个时遇到了符号“WHEN”:(开始情况下声明为goto退出如果循环mod空pragma在<标识符><<继续关闭当前删除读取锁定插入打开回滚保存点集合sql执行提交forall合并管道清除符号“case”被替换为“WHEN”以继续。 ]和[PLS-00103:在遇到以下情况时遇到符号“;”]案例] – user2648669

+0

我认为ORDER_UNIT字段位于ITEMS表格上,请再试一次 –

+0

我得到的错误相同 – user2648669

0

存在一些语法错误。
能否请您尝试运行下面的代码

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; 
+0

ITEMS等于(Item_code_orders)我得到“ORA-00906缺少左括号”错误 – user2648669

+0

编辑答案请立即尝试 – Harshit

+0

现在我得到ORA-04076:无效NEW或OLD规范! – user2648669

相关问题