在SQLPlus中,当你输入一个SQL command 或者PL/SQL块(如匿名块、存储过程、函数或包体)并执行它后,SQLPlus会将该PL/SQL块保存在其缓冲区中。之后,如果你仅仅输入Run或一个斜杠(/)并按回车,SQLPlus会重新执行缓冲区中最后一条执行的PL/SQL块。这实际上是SQLPlus的一个便捷特性,用于快速重新执行之前的PL/SQL代码,而无需重新输入整个块。
参考官方文档地址
https://docs.oracle.com/en/database/oracle/oracle-database/23/sqpug/RUN.html#GUID-C0504161-EFF9-4AF0-9F59-074F17B0CB85
1、语法Syntax
R[UN]
Lists and executes the SQL command or PL/SQL block currently stored in the SQL buffer.
列出并执行当前存储在SQL缓冲区中的SQL命令或PL/SQL块。
2、示例Examples
RUN causes the last line of the SQL buffer to become the current line.
The slash command (/) functions similarly to RUN, but does not list the command in the SQL buffer on your screen. The SQL buffer always contains the last SQL statement or PL/SQL block entered.
2.1 SQL statement
TESTUSER@FREEPDB1> select * from dept;
DEPTNO DNAME LOC
---------- -------------------- ------------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
TESTUSER@FREEPDB1> run
1* select * from dept
DEPTNO DNAME LOC
---------- -------------------- ------------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
TESTUSER@FREEPDB1> /
DEPTNO DNAME LOC
---------- -------------------- ------------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
2.2 PL/SQL block
TESTUSER@FREEPDB1> DECLARE i NUMBER;
2 BEGIN
3 FOR i IN 1..5 LOOP
4 DBMS_OUTPUT.PUT_LINE('i的值为: ' || i);
5 END LOOP;
6 END;
7 /
i的值为: 1
i的值为: 2
i的值为: 3
i的值为: 4
i的值为: 5
PL/SQL procedure successfully completed.
-- 使用run
TESTUSER@FREEPDB1> run
1 DECLARE i NUMBER;
2 BEGIN
3 FOR i IN 1..5 LOOP
4 DBMS_OUTPUT.PUT_LINE('i的值为: ' || i);
5 END LOOP;
6* END;
i的值为: 1
i的值为: 2
i的值为: 3
i的值为: 4
i的值为: 5
PL/SQL procedure successfully completed.
-- 使用 /
TESTUSER@FREEPDB1> /
i的值为: 1
i的值为: 2
i的值为: 3
i的值为: 4
i的值为: 5
PL/SQL procedure successfully completed.
3、总结
RUN使SQL缓冲区的最后一次执行的SQL语句或PL/SQL(如匿名块、存储过程、函数或包体)成为当可以执行命令。
斜线命令(/)的功能类似于RUN,但不会在屏幕上的SQL缓冲区中列出该命令。SQL缓冲区始终包含最后输入的SQL语句或PL/SQL块。