在Exception语句中执行sql文件

csu*_*azo 2 oracle oracle-11g-r2 plsql

嗨,当我执行假脱机文件时,我试图在异常语句中执行 SQL 文件。

我有这样的事情:

column dt new_value _dt 
select to_char(sysdate,'yyyymmdd_hh24mi') dt from dual; 
set line 10000;
set pagesize 50000;
set serveroutput on;

spool .\backup\backup.sql

select dbms_metadata.get_ddl(object_type, object_name)
from user_objects
where object_type in ('FUNCTION')
and object_name = 'TEST_TABLE';

Spool Off

spool .\!Run_&_dt..txt

BEGIN
    raise_application_error( -20001, 'This is a custom error' );
EXCEPTION 
    WHEN OTHERS THEN
        Prompt ./backup/backup.sql
        @ ./backup/backup.sql
END;

Spool Off
Run Code Online (Sandbox Code Playgroud)

我得到下一个错误:

>> BEGIN
     raise_application_error( -20001, 'This is a custom error' );
EXCEPTION 
    WHEN OTHERS THEN
        Prompt ./backup/backup.sql
        @ ./backup/backup.sql
END;


Spool Off
Error at line 18
ORA-06550: line 6, column 11:
PLS-00103: Encountered the symbol "/" when expecting one of the following:

    <an identifier> <a double-quoted delimited-identifier>
    current delete exists prior
ORA-06550: line 6, column 49:
PLS-00103: Encountered the symbol "SQL" when expecting one of the following:

    <an identifier> <a double-quoted delimited-identifier>
    current delete exists prior
Run Code Online (Sandbox Code Playgroud)

我不知道,提前谢谢。

此致。

小智 5

所以这有点令人困惑——您正在编写一个 SQL*Plus 脚本,它接受 SQL*Plus 命令以及 SQL 命令和 PL/SQL 块。

SQL*Plus 命令在您的客户端机器上本地执行,因此它们可以访问您本地文件系统上的其他脚本。

BEGIN..END块内的所有内容都是PL/SQL 代码,它的行为与脚本的其余部分不同,因为它是一种不同的语言。整个代码块被发送到服务器并在那里执行,数据库不知道如何解释prompt和等 sqlplus 命令@

有多种方法可以让 PL/SQL 块在数据库服务器上执行脚本-有关更多详细信息,请参阅此答案- 但我认为这并不是您真正想要的。

很难从您的示例中说出您想要什么,但我建议您考虑使用 shell 脚本处理 sqlplus 错误- 将其拆分为两个 sqlplus 脚本,然后运行第一个脚本,检查返回码,如果出现错误,运行您的第二个脚本。Sqlplus 本身并没有太多的内置异常处理方式。