sql - Oracle SQL中的脚本错误

标签 sql oracle plsql syntax-error sqlplus

我有一个具有以下语句的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中执行此操作,因为“立即关闭”不是PLSQL或SQL命令,而是SQLplus命令。该问题的答案描述了一种有条件执行脚本的方法:
SQLplus decode to execute scripts

基本上,根据您的主键的值,您可以选择两个脚本名称之一,然后以该名称执行脚本。

基于前面示例中的代码。

  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

关于sql - Oracle SQL中的脚本错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39701508/

相关文章:

sql - Talend:使用已定义的字符串/参数查询数据库

oracle - 从 PL/SQL 函数返回多个值

mysql 两个唯一的列,如果插入到另一个列中,一个列中的值将被视为重复

mysql - 使用左外连接连接两个mysql结果集

SQL 与...更新

java - 将 'select from dual' 作为参数传递给 java 可调用语句

tsql - 忽略 jdbc 批处理中的错误语句

oracle - 为架构设置 PLSQL_CCFLAGS

sql - 如何从另一个表中获取具有最旧值的项目列表

php - MySQL - 如何从最小/最大值获取最小/最大日期