2013-08-12 64 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

相關問題