SQLPLUS小技巧带行号PLSQL的粘贴

前两天写了一篇如何在SQLPLUS中粘贴SQL语句,但是改方法对于SQL有效,对于PL/SQL语句则存在一些小问题。
SQLPLUS小技巧带行号SQL的粘贴:https://yangtingkun.net/?p=1167
还是在sqlplus中粘贴带行号的问题,对于PL/SQL,之前给出的方法存在问题:

SQL> DECLARE
  2  V_NUM NUMBER;
  3  BEGIN
  4  FOR I IN 1..10000 LOOP
  5  NULL;
  6  END LOOP;
  7  END;
  8  /
PL/SQL PROCEDURE successfully completed.
SQL> DECLARE
  2  .
SQL>   2  V_NUM NUMBER;
SQL>   3  BEGIN
SQL>   4  FOR I IN 1..10000 LOOP
SQL>   5  NULL;
SQL>   6  END LOOP;
SQL>   7  END;
SQL> R
  1  DECLARE
  2   V_NUM NUMBER
  3   BEGIN
  4   FOR I IN 1..10000 LOOP
  5   NULL
  6   END LOOP
  7*  END
 BEGIN
 *
ERROR at line 3:
ORA-06550: line 3, COLUMN 2:
PLS-00103: Encountered the symbol "BEGIN" WHEN expecting one OF the following:
:= . ( @ % ; NOT NULL range DEFAULT CHARACTER
The symbol ";" was substituted FOR "BEGIN" TO continue.
ORA-06550: line 6, COLUMN 2:
PLS-00103: Encountered the symbol "END" WHEN expecting one OF the following:
;

其实导致问题的根源很简单,就是分号;造成的。对于SQL语句,分号作为语句的结束符,而对于PL/SQL语句则不然,因此在粘贴的过程中,所有的分号丢失,造成了PL/SQL语句的错误。
解决这个问题很简单,方法和粘贴SQL并无差别,唯一需要做的是,在粘贴PL/SQL代码前将SQL语句的终结符改为/:

SQL> SHOW SQLT
sqlterminator ";" (hex 3b)
SQL> SET SQLT '/'
SQL> DECLARE
  2  .
SQL>   2  V_NUM NUMBER;
SQL>   3  BEGIN
SQL>   4  FOR I IN 1..10000 LOOP
SQL>   5  NULL;
SQL>   6  END LOOP;
SQL>   7  END;
SQL> R
  1  DECLARE
  2   V_NUM NUMBER;
  3   BEGIN
  4   FOR I IN 1..10000 LOOP
  5   NULL;
  6   END LOOP;
  7*  END;
PL/SQL PROCEDURE successfully completed.
This entry was posted in ORACLE and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *