Oracle 中的脚本错误 SQL

Script Error in Oracle SQL

我有一个 sql 文件,其中包含以下语句:

BEGIN
if (&&masterKey = 1) then
shutdown immediate;
startup restrict;
end if;
END;
/

在另一个 SQL 文件中 (defineVariables.sql) 我已经声明了变量 masterKey。

DEFINE masterKey = 0;

并在此处使用

导入 sql
@defineVariables.sql

当我执行脚本时出现以下错误。我不确定是不是因为我使用了 shutdown 语句?有人可以帮我解决这个问题吗?

错误信息:

SQL> BEGIN
  2  if (&&masterKey = 1) then
  3  shutdown immediate;
  4  startup restrict;
  5  end if;
  6  END;
  7  /
old   2: if (&&masterKey = 1) then
new   2: if (0 = 1) then
shutdown immediate;
         *
ERROR at line 3:
ORA-06550: line 3, column 10:
PLS-00103: Encountered the symbol "IMMEDIATE" when expecting one of the
following:
:= . ( @ % ;
ORA-06550: line 5, column 1:
PLS-00103: Encountered the symbol "END" when expecting one of the following:
begin function pragma procedure subtype type <an identifier>
<a double-quoted delimited-identifier> current cursor delete
exists prior

您不能在 PLSQL 中执行此操作,因为 "shutdown immediate" 不是 PLSQL 或 SQL 命令,而是 SQLplus 命令。在这个问题的答案中描述了一种实现脚本条件执行的方法: SQLplus decode to execute scripts

基本上,根据您的主密钥的值,您 select 两个脚本名称之一,然后使用该名称执行脚本。

基于前面示例中的代码。

  sql>  variable flag varchar2(7);
  sql>  exec :flag := '&&masterKey';
  sql>  column our_script new_value script noprint;
  sql>  select decode(:flag, '1', 
                   'c:\sqlplus\shutdown_script.sql', 
                   'c:\sqlplus\do_not_shutdown.sql'
                   ) our_script
    from dual;

执行

sql> @&script;

shutdown_script 会是

prompt shutting down
shutdown immediate;

do_not_shutdown 脚本将是

prompt Not shutting down