2016-03-15 66 views
0

我編寫了一個包含if..else if..end的過程,如下所示。 雖然編譯它引發錯誤作爲If..else在Oracle中不工作

錯誤(103,8):PLS-00103:出現符號 「LOOP」 期待當下列中的一個:如果 錯誤(113,4):PLS-00103 :出現符號「檔案結尾」期待之一時,以下:到底要不要編譯最終實例化爲了重寫靜態成員的構造圖

我無法找到錯誤的原因,因爲我認爲我有正確關閉所有IF。任何人都可以請求幫助。以下是我的整個過程

CREATE OR REPLACE PROCEDURE proc_ntitems_update 
AS 
    CURSOR cur_systeminfo 
    IS 
     SELECT location, 
      childseq, 
      btproduct, 
      product, 
      questionid 
     FROM systeminfo; 

    vproduct   tblsysteminfo_products.product%TYPE; 
    vquestionid  tblsysteminfo_products.questionid%TYPE; 
    vlocation  systeminfo.location%TYPE; 
    vchildseq  systeminfo.childseq%TYPE; 
    verror   VARCHAR2 (200); 
    consrgsitename VARCHAR2 (100); 
    consrgsite  VARCHAR2 (100); 
    conmgsitename VARCHAR2 (100); 
    intname   NUMBER; 
    vsicount   NUMBER; 
BEGIN 
    OPEN cur_systeminfo; 

    LOOP 
     FETCH cur_systeminfo 
     INTO vlocation, vchildseq, vbtproduct, vproduct, vquestionid; 

     EXIT WHEN cur_systeminfo%NOTFOUND; 

     IF INSTR (vlocation, '_') > 0 
     THEN 
     vquestionid := consrgsitename; 

     IF SUBSTR (vlocation, 1, LENGTH (consrgsite)) = consrgsite 
     THEN 
      intname := LENGTH (consrgsite); 
      vquestionid := consrgsitename; 
     ELSE 
      IF SUBSTR (vlocation, 1, LENGTH (consrg50site)) = consrg50site 
      THEN 
       intname := LENGTH (consrgsite); 
       vquestionid := consrgsitename; 
       vlocation := 
        vlocation 
        || SUBSTR (vlocation, intname + 1, LENGTH (vlocation)); 
      --Check if this item is for a DATA site. These have a Location of locations_data_n where n is the site number. 

      ELSE 
       IF LOWER (SUBSTR (vlocation, 1, 14)) = 'locations_data' 
       THEN 
        vsitename := 'Yes'; 
        vquestionid := 'txtLocationName'; 
       ELSE 
        IF  LOWER (SUBSTR (vlocation, 1, 14)) = 'locations_data' 
        AND vchildseq = '0' 
        THEN 
        BEGIN 
         SELECT COUNT (location) 
          INTO vsicount 
          FROM systeminfo 
         WHERE LOWER (vlocation) = 'locations_1'; 
        EXCEPTION 
         WHEN nodatafound 
         THEN 
          verror := 
           ' proc_ntitems_update ' 
           || SUBSTR (
            TO_CHAR (SQLCODE) || ' - ' || SQLERRM, 
            1, 
            480); 
         WHEN OTHERS 
         THEN 
          verror := 
           ' proc_ntitems_update ' 
           || SUBSTR (
            TO_CHAR (SQLCODE) || ' - ' || SQLERRM, 
            1, 
            480); 
        END; 

        IF vsicount = 0 
        THEN 
         vsitename := 'No'; 
         intname := LENGTH (vlocation); 
         vquestionid := 'txtLocationName'; 
        END IF; 
        ELSE 
        IF  LOWER (SUBSTR (vlocation, 1, 14)) = 
           'locations_data' 
         AND vchildseq > '0' 
        THEN 
         vquestionid := 'txtLocationName'; 
        ELSE 
         IF LOWER (SUBSTR (vlocation, 1, 5)) = 'ripe_' 
         THEN 
          vremotesitenumber := 
           SUBSTR (
           vlocation, 
           -1, 
           (LENGTH (vloaction) - INSTR (vlocation, '_'))); 
          vproduct := 'MMRemote'; 
          vquestionid := 'cboRemoteType' || vremotesitenumber; 
          vsitename := vecvalue || ' ' || vremotesitenumber; 
         ELSE 
          IF LOWER (
           SUBSTR (vlocation, 
             1, 
             LENGTH (conmerchandisesite))) = 
           conmerchandisesite 
          THEN 
           vsitename := conmerchandisesite; 
          ELSE 
           vsitename := btproduct; 
          END IF; 


          BEGIN 
           UPDATE tblntitems 
           SET location = vlocation, 
            childseq = vchildseq, 
            sitename = vbtproduct, 
            product = vproduct, 
            questionid = vquestionid 
           WHERE questionid = vquestionid; 
          EXCEPTION 
           WHEN OTHERS 
           THEN 
           verror := 
             'proc_ntitems_update ' 
            || SUBSTR (
              TO_CHAR (SQLCODE) 
              || ' - ' 
              || SQLERRM, 
              1, 
              480); 
          END; 
         END IF; 
        END loop; 

    CLOSE cur_systeminfo; 

     EXCEPTION 
     WHEN OTHERS 
     THEN 
       verror := 'proc_ntitems_update ' || substr (to_char (sqlcode) || ' - ' || sqlerrm, 1, 480); 

     INSERT INTO log_import_errors VALUES ('sysdate', verror); 
END; 
+2

在PL/SQL中,我們使用'elsif'而不是'else if'來表示這種邏輯。你可以使用else if,但是你開始一個新的if塊,並且每個實例都需要一個end if。 –

回答

2

您正在創建嵌套的if-then-else語句;您在同一行上有ELSEIF,但它們是單獨的控制級別。這可能是更容易理解,如果設置有壓痕以顯示嵌套你要去哪裏錯了,因爲:

LOOP 
    ... 
    IF Instr(Vlocation,'_') >0 THEN 
    ... 
    IF SUBSTR(Vlocation,1,LENGTH(Consrgsite)) = Consrgsite THEN 
     ... 
    ELSE 
     IF SUBSTR(Vlocation,1,LENGTH(Consrg50site)) = Consrg50site THEN 
     ... 
     ELSE 
     IF Lower(SUBSTR(Vlocation,1,14)) = 'locations_data' THEN 
      ... 
     ELSE 
      IF Lower(SUBSTR(Vlocation,1,14)) = 'locations_data' AND Vchildseq = '0' THEN 
      ... 
      IF Vsicount = 0 THEN 
       ... 
      END IF; 
      ELSE 
      IF Lower(SUBSTR(Vlocation,1,14)) = 'locations_data' AND Vchildseq > '0' THEN 
       ... 
      ELSE 
       IF Lower(SUBSTR(Vlocation,1,5)) = 'ripe_' THEN 
       ... 
       ELSE 
       IF Lower(SUBSTR(Vlocation,1,LENGTH(Conmerchandisesite))) = Conmerchandisesite THEN 
        ... 
       ELSE 
        ... 
       END IF; 
       ... 
       END IF; 
      END LOOP; 

如果您目前有END LOOP它希望看到END IF,這將匹配IF Lower(SUBSTR(Vlocation,1,14)) = 'locations_data' AND Vchildseq > '0' THEN線。

正如託尼·安德魯斯在評論中所說,你需要有一個END IF來匹配每個IF。您目前擁有六個級別的控制權,這些控制權以IF打開,但未以END IF關閉。

你似乎在期待它像ELSIF,你可以用它來代替工作:

LOOP 
    ... 
    IF Instr(Vlocation,'_') >0 THEN 
    ... 
    IF SUBSTR(Vlocation,1,LENGTH(Consrgsite)) = Consrgsite THEN 
     ... 
    ELSIF SUBSTR(Vlocation,1,LENGTH(Consrg50site)) = Consrg50site THEN 
     ... 
    ELSIF Lower(SUBSTR(Vlocation,1,14)) = 'locations_data' THEN 
     ... 
    ELSIF Lower(SUBSTR(Vlocation,1,14)) = 'locations_data' AND Vchildseq = '0' THEN 
     ... 
     IF Vsicount = 0 THEN 
     ... 
     END IF; 
    ELSIF Lower(SUBSTR(Vlocation,1,14)) = 'locations_data' AND Vchildseq > '0' THEN 
     ... 
    ELSIF Lower(SUBSTR(Vlocation,1,5)) = 'ripe_' THEN 
     ... 
    ELSIF Lower(SUBSTR(Vlocation,1,LENGTH(Conmerchandisesite))) = Conmerchandisesite THEN 
     ... 
    ELSE 
     ... 
    END IF; 
    ... 
    END IF; 
END LOOP; 

你也可以使用一個searched CASE expression代替IF-THEN-ELSIF-ELSE,但如果同樣的效果。

查看更多about conditional control statements