Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在sql plus中有条件地调用sql脚本_Sql_Oracle_Cmd_Sqlplus - Fatal编程技术网

在sql plus中有条件地调用sql脚本

在sql plus中有条件地调用sql脚本,sql,oracle,cmd,sqlplus,Sql,Oracle,Cmd,Sqlplus,我有两个脚本需要根据数据库中是否存在表来执行 因此,我创建了第三个脚本,如下所示,它检查条件并调用相应的脚本。 [因为我的安装程序无法访问db,安装时只能调用一个脚本] declare cnt number; begin select count(*) into cnt from all_tables where table_name = 'VQ_REPORT_LAUNCHER'; if (cnt>0) then begin @VQ_Alter_Scrip

我有两个脚本需要根据数据库中是否存在表来执行

因此,我创建了第三个脚本,如下所示,它检查条件并调用相应的脚本。 [因为我的安装程序无法访问db,安装时只能调用一个脚本]

declare
  cnt number;
begin
  select count(*) 
  into cnt
  from all_tables where table_name = 'VQ_REPORT_LAUNCHER';


if (cnt>0) then
  begin
    @VQ_Alter_Script.sql;
  end;
else
  begin
    @VQ_Create_Script.sql;
  end;
end if;
结束

我得到下面的错误- 第10行出错: ORA-06550:第10行第1列: PLS-00103:在预期以下情况时遇到符号“创建”:

注意-当我直接从sql plus执行我的创建/更改脚本时,它可以工作。
只有当我尝试使用IF-ELSE通过第三个脚本执行它们时,才会在sql plus中出现上述错误。

您可以使用替换变量来决定运行哪个脚本

column script_name new_value script_name

select case count(*)
         when 0 then 'VQ_Create_Script.sql'
         else 'VQ_Alter_Script.sql'
       end as script_name
from all_tables
where table_name = 'VQ_REPORT_LAUNCHER';

@&script_name
或者,如果名称仅部分更改,您可以执行以下操作:

column script_type new_value script_type

select case count(*) when 0 then 'Create' else 'Alter' end as script_type
from all_tables
where table_name = 'VQ_REPORT_LAUNCHER';

@VQ_&script_type._Script.sql
如果需要,可以在查询部分周围添加设置,如
set termout off
set termout on
,以隐藏查询部分,并使用
set verify
决定是否显示替换


根据您作为哪个用户运行此操作,您可能希望检查
user\u tables
,而不是
all\u tables
,或者将预期的表所有者包括在筛选器中,因此,您不会意外地在错误的模式中拾取同名的表。

如果不使用不同的近似值,则无法从PL/sql调用sql脚本,例如DBMS_SCHEDULER外部API for sql_SCRIPT作业类型将从安装程序调用脚本,我们只需检查该表是否存在并相应地调用另一个脚本。这不能在.sql中完成吗?