2015-09-06 51 views
0

我試圖用IF.. ELSE條件和語句來更新庫存的單位,但是當試圖運行它,我得到一個錯誤:下面的if/else在PL SQL循環

PLS-00103: Encountered the symbol "LOOP" when expecting one of the following: 
    if 

代碼,我它運行在一個循環吐出所有的數據,直到用完:

DECLARE 

--Declare cursor 
CURSOR part_cur 
IS 
SELECT PARTNUMBER, PARTDESC, ITEMCLASS, UNITSONHAND, UNITSONHAND 
FROM PART 
ORDER BY UPPER(PARTNUMBER); 

--Declare variables 
v_PartNumber PART.PARTNUMBER%TYPE; 
v_PartDesc PART.PARTDESC%TYPE; 
v_ItemClass PART.ITEMCLASS%TYPE; 
v_UnitsOnHand PART.UNITSONHAND%TYPE; 
v_nUnitsOnHand PART.UNITSONHAND%TYPE; 

BEGIN 
OPEN part_cur; 
LOOP 

--Initialise variables 
    FETCH part_cur INTO v_PartNumber, v_PartDesc, v_ItemClass, v_UnitsOnHand, v_nUnitsOnHand; 
     EXIT WHEN part_cur%NOTFOUND; 

--Perform Calculations 
    IF ITEMCLASS = 'AO' AND UNITSONHAND <20 THEN v_nUnitsOnHand := UNITSONHAND + 5; 
ELSE IF ITEMCLASS = 'AO' AND UNITSONHAND <100 THEN v_nUnitsOnHand := UNITSONHAND + 10; 
ELSE IF ITEMCLASS = 'AO' AND UNITSONHAND >=100 THEN v_nUnitsOnHand := UNITSONHAND + 25; 
END IF; 

--Format for output 

--Output results 
     DBMS_OUTPUT.PUT_LINE('Part ' || UPPER(v_PartNumber) || ' - ' || INITCAP(v_PartDesc) || ' of item class ' || UPPER(v_ItemClass) || ' had a total of ' || v_UnitsOnHand || ' units on hand, 
     and now has ' || v_nUnitsOnHand || ' units on hand after the database was updated. 
     '); 
END LOOP; 
CLOSE part_cur; 
END; 

任何幫助或指針將不勝感激,謝謝。用於訪問這些字段

record_var.v_PartNumber, etc 

回答

-1

試試這個:

FOR record_var in part_cur 
LOOP 
    {...statements...} 
END LOOP; 

之後,你可以寫。

2

IF聲明目前看起來是這樣的:

IF ITEMCLASS = 'AO' AND UNITSONHAND <20 THEN 
    v_nUnitsOnHand := UNITSONHAND + 5; 
ELSE 
    IF ITEMCLASS = 'AO' AND UNITSONHAND <100 THEN 
     v_nUnitsOnHand := UNITSONHAND + 10; 
    ELSE 
     IF ITEMCLASS = 'AO' AND UNITSONHAND >=100 THEN 
      v_nUnitsOnHand := UNITSONHAND + 25; 
     END IF; 

所以它的缺席兩場END IF秒。所以當Oracle看到END LOOP時,它會抱怨,因爲它預計會有END IF

爲了解決這個問題,只是ELSIF同時替換您的ELSE IF S的,這是出於這樣的目的的特殊關鍵字:

IF ITEMCLASS = 'AO' AND UNITSONHAND <20 THEN 
    v_nUnitsOnHand := UNITSONHAND + 5; 
ELSIF ITEMCLASS = 'AO' AND UNITSONHAND <100 THEN 
    v_nUnitsOnHand := UNITSONHAND + 10; 
ELSIF ITEMCLASS = 'AO' AND UNITSONHAND >=100 THEN 
    v_nUnitsOnHand := UNITSONHAND + 25; 
END IF; 
+0

哦歡呼聲謝謝,我不知道有多深我的'ELSE .. IF'陳述正在進行中。 – user3521826

+0

@ user3521826:不客氣! – ruakh