在 sql plus 中有条件地调用 sql 脚本
Conditionally calling sql scripts in sql plus
我有两个脚本需要执行,具体取决于 table 是否存在于我的数据库中。
所以我创建了如下所示的第三个脚本,它检查条件并调用相应的脚本。
[因为我的安装程序无法访问数据库,并且安装时只能调用一个脚本]
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:在期望以下之一时遇到符号“CREATE”:
注意 - 当我直接从 sql 执行我的 create/alter 脚本时它起作用了。
只有当我尝试使用 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_tables
而不是 all_tables
,或者包括预期的 table 所有者作为一部分的过滤器,这样你就不会不小心在错误的模式中选择了同名的 table。
我有两个脚本需要执行,具体取决于 table 是否存在于我的数据库中。
所以我创建了如下所示的第三个脚本,它检查条件并调用相应的脚本。 [因为我的安装程序无法访问数据库,并且安装时只能调用一个脚本]
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:在期望以下之一时遇到符号“CREATE”:
注意 - 当我直接从 sql 执行我的 create/alter 脚本时它起作用了。 只有当我尝试使用 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_tables
而不是 all_tables
,或者包括预期的 table 所有者作为一部分的过滤器,这样你就不会不小心在错误的模式中选择了同名的 table。