PLSQL - Error in associative array

时间:2015-06-15 14:54:24

标签: oracle plsql associative-array

Im trying to delete a set of tables and afterwards I want to recreate them using as select from. For couriousity I wanted to do this with an associative array. Unfortunately something is messed up, several errors appear and I can't find the reasons. This is the code:

DECLARE
TYPE t_tbl
IS
  TABLE OF VARCHAR(100) INDEX BY VARCHAR2(100);
  L_Tbl T_Tbl;
  l_key VARCHAR2(100);
BEGIN
  l_tbl('tableA') := 'table1';
  l_tbl('tableB') := 'table2';
  L_Tbl('tableC') := 'table3';
  l_tbl('tableD') := 'table4';

  l_key := l_tbl.first;
  LOOP
    BEGIN
      EXIT WHEN L_Key IS NULL;
      Dbms_Output.Put_Line('Dropping TABLE '|| L_Key ||);
      EXECUTE Immediate 'DROP TABLE ' || L_Key;
      dbms_output.put_line(l_key ||' '|| l_tbl(l_key));
    -- Catch exception if table does not exist
    EXCEPTION
    WHEN OTHERS THEN
      IF SQLCODE != -942 THEN
        Raise;
      END IF;
      EXECUTE IMMEDIATE 'create table schema1.' ||l_key||' as select * from schema2.'||l_tbl(l_key)||;
      l_key := l_tbl.next(l_key);
    END LOOP;
  End;
END;

I get these errors:

ORA-06550: line 17, column 56:
PLS-00103: Encountered the symbol ")" when expecting one of the following:

   ( - + case mod new null <an identifier>
   <a double-quoted delimited-identifier> <a bind variable>
   continue avg count current max min prior sql stddev sum
   variance execute forall merge time timestamp interval date
   <a string literal with character set specification>
   <a number> <a single-quoted SQL string> pipe
   <an alternatively-quoted string literal with character set specification>
   <an alternatively-quoted SQL
ORA-06550: line 26, column 102:
PLS-00103: Encountered the symbol ";" when expecting one of the following:

   ( - + case mod new null <an identifier>
   <a double-quoted delimited-identifier> <a bind variable>
   continue avg count current max min prior sql stddev su
ORA-06550: line 29, column 6:
PLS-00103: Encountered the symbol ";" when expecting one of the following:

   loop
The symbol "loop" was substituted for ";" to continue.
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

Could someone give me a hint please? Thank you in advance!

2 个答案:

答案 0 :(得分:2)

You have some unterminated append operations || on lines:

Dbms_Output.Put_Line('Dropping TABLE '|| L_Key ||);

And

EXECUTE IMMEDIATE 'create table schema1.' ||l_key||' as select * from schema2.'||l_tbl(l_key)||;

Get rid of the || at the end. Also the way you are using LOOP is incorrect. Refer example:

while elem is not null loop
    dbms_output.put_line(elem || ': ' || var_assoc_varchar(elem));
    elem := var_assoc_varchar.next(elem);   
end loop;

答案 1 :(得分:1)

Remove || at the end of the line in line 17 and line 25.

And also ending loop before ending the block begin....end. get begin before loop or get end before end loop.

           l_key := l_tbl.next(l_key);
      END LOOP;
  END;