使用plsql过程编译错误

时间:2015-01-08 18:06:46

标签: plsql ora-06550

下面是一个由其他人编写的plsql程序,我已经要求修复其编译错误并运行正确。

 PROCEDURE DropMyTable IS
  x                number;  
  TYPE cur_typ IS REF CURSOR;
 --type NAME_TBL is table of VARCHAR2(30);
 --tab_Table                 NAME_TBL;
 tab_Table                VARCHAR2S(30);
 stmt                     VARCHAR2(4096);
  stmt2                    VARCHAR2(4096);
    outerCur                 cur_typ;
      rows                     NATURAL := 1000;
    TABLE_DOES_NOT_EXIST     EXCEPTION;
   PRAGMA EXCEPTION_INIT(TABLE_DOES_NOT_EXIST, -942);
  BEGIN
 stmt2 := 'select distinct G_Tab  from G_SERVE  where G_TYPE=''CAP'''; 
  --Begin
   OPEN outerCur FOR stmt2;
   LOOP
    FETCH outerCur BULK COLLECT INTO
      tab_Table LIMIT rows;
   EXIT WHEN tab_Table.COUNT = 0;

   FOR i IN 1..tab_Table.COUNT LOOP
    --------------------------------------------------------------------
      -- Drop the  tables in G_SERVE.G_TAB
      DBMS_OUTPUT.PUT_LINE('*** Drop  table: ' || tab_Table(i) || ' ***');
      -- BEGIN
      --First drop the optable      
      stmt := 'DROP TABLE ' || tab_Table(i) || '_OPTAB CASCADE CONSTRAINTS';
      DBMS_OUTPUT.PUT_LINE(stmt);
      EXECUTE IMMEDIATE stmt;  
      --drop the base table
      stmt := 'DROP TABLE ' || tab_Table(i) || ' CASCADE CONSTRAINTS';
    DBMS_OUTPUT.PUT_LINE(stmt);
    EXECUTE IMMEDIATE stmt;
      --drop the Package
      stmt := 'DROP PACKAGE  ' || tab_Table(i) || '_PKG';
      DBMS_OUTPUT.PUT_LINE(stmt);
      EXECUTE IMMEDIATE stmt;     
    EXCEPTION
  -- When the table, optab and package  doesn't exist,
  -- ignore error "ORA-00942: table, optab or package does not exist".
   WHEN TABLE_DOES_NOT_EXIST THEN
    DBMS_OUTPUT.PUT_LINE('table, optab or package does not exist');
    DBMS_OUTPUT.PUT_LINE(CHR(9));
   --Re-raises any other errors.
   WHEN OTHERS THEN
   DBMS_OUTPUT.PUT_LINE('Error!');
    RAISE;
     END;
  END LOOP;
 END LOOP;
 --END;
 END DropMyTable;

首先,我已经从乞讨中注释掉了第4行和第5行代码,并将其更改为现在的样子(从顶部开始的第6行)。

我已经注释掉了2个BEGIN关键词的其他内容首先注释掉了#34; BEGIN"

之后可以看到
  stmt2 := 'select distinct gao_table  from GAO_SERVICESIFACE  where GAO_TYPE=''CAPABILITY''';

和第二个" BEGIN"关键字已在以下代码行之后注释掉。

 DBMS_OUTPUT.PUT_LINE('*** Drop  table: ' || tab_Table(i) || ' ***');

因为我怀疑编写这段代码的人放了不必要的" BEGIN"关键字(我道歉,如果我错了,因为我不是plsql专家。但是做了所有评论的事情我摆脱了这么多错误)。

但是当我编译上面的代码时,我仍然会遇到2个错误。

1)

ORA-06550: line 27, column 7:
PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following:
 ;
 ( begin case declare end exit for goto if loop mod null
 pragma raise return select update while with <an identifier>
 <a double-quoted delimited-identifier> <a bind variable> <<
 continue close current delete fetch lock insert open rollback
 savepoint set sql execute commit forall merge pipe purge  

2)

ORA-06550: line 38, column 9:
PLS-00103: Encountered the symbol "LOOP" when expecting one of the following:
   ;
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

1 个答案:

答案 0 :(得分:1)

你已经在内部循环中注释掉了BEGIN,但没有注明其关联的EXCEPTION和END。你需要那个开始。此外,您无法将数组更改为标量变量,而代码仍需要数组(引用tab_Table(i)等)。

代码的体面缩进总是让它更容易理解:

PROCEDURE DropMyTable IS
  x                number;  
  TYPE cur_typ IS REF CURSOR;
  type NAME_TBL is table of VARCHAR2(30);
  tab_Table                 NAME_TBL;
  --tab_Table                VARCHAR2S(30);
  stmt                     VARCHAR2(4096);
  stmt2                    VARCHAR2(4096);
  outerCur                 cur_typ;
  rows                     NATURAL := 1000;
  TABLE_DOES_NOT_EXIST     EXCEPTION;
  PRAGMA EXCEPTION_INIT(TABLE_DOES_NOT_EXIST, -942);
BEGIN
  stmt2 := 'select distinct G_Tab  from G_SERVE  where G_TYPE=''CAP'''; 
  --Begin
  OPEN outerCur FOR stmt2;
  LOOP
    FETCH outerCur BULK COLLECT INTO
      tab_Table LIMIT rows;
    EXIT WHEN tab_Table.COUNT = 0;

    FOR i IN 1..tab_Table.COUNT LOOP
      --------------------------------------------------------------------
      -- Drop the  tables in G_SERVE.G_TAB
      DBMS_OUTPUT.PUT_LINE('*** Drop  table: ' || tab_Table(i) || ' ***');
      BEGIN
        --First drop the optable      
        stmt := 'DROP TABLE ' || tab_Table(i) || '_OPTAB CASCADE CONSTRAINTS';
        DBMS_OUTPUT.PUT_LINE(stmt);
        EXECUTE IMMEDIATE stmt;  
        --drop the base table
        stmt := 'DROP TABLE ' || tab_Table(i) || ' CASCADE CONSTRAINTS';
        DBMS_OUTPUT.PUT_LINE(stmt);
        EXECUTE IMMEDIATE stmt;
        --drop the Package
        stmt := 'DROP PACKAGE  ' || tab_Table(i) || '_PKG';
        DBMS_OUTPUT.PUT_LINE(stmt);
        EXECUTE IMMEDIATE stmt;     
      EXCEPTION
        -- When the table, optab and package  doesn't exist,
        -- ignore error "ORA-00942: table, optab or package does not exist".
        WHEN TABLE_DOES_NOT_EXIST THEN
          DBMS_OUTPUT.PUT_LINE('table, optab or package does not exist');
          DBMS_OUTPUT.PUT_LINE(CHR(9));
        --Re-raises any other errors.
        WHEN OTHERS THEN
          DBMS_OUTPUT.PUT_LINE('Error!');
          RAISE;
      END;
    END LOOP;
  END LOOP;
--END;
END DropMyTable;

我恢复了BEGIN并恢复了阵列。我不知道是否还有其他错误。