我可以以编程方式退出Firebird脚本

问题描述 投票:2回答:1

请考虑以下脚本:

set term ^;

exit
^

execute block
as
begin
   execute statement 'this will fail';
end
^

exit完全有效,并确实导致脚本执行结束。至少在我正在测试它的IBExpert中。但我想以编程方式执行此操作。

set term ^;

execute block
as
begin
   if (exists(select 1 from sometable where somevalue = 1)) then begin
      -- This only exits the block, not the script
      exit;
   end
end
^

execute block
as
begin
   execute statement 'this will fail';
end
^

我的第一个例子中的exit是有效的Firebird还是IBExpert处理它本身?有条件地退出整个脚本有不同的方法吗?

sql firebird firebird2.5 ibexpert
1个回答
2
投票

您在脚本中使用exit令人困惑,在Firebird语句中使用exit(特别是execute block)。脚本中的普通exit被IBExpert解释为停止脚本的信号。

但是,当exitexecute block的一部分时,它不是脚本的一部分,它是发送到Firebird服务器执行的语句的一部分,并且它对脚本本身的执行没有影响。

execute block语句中的代码是PSQL,其中EXIT具有特定含义:

EXIT语句导致执行过程或触发器从代码中的任何点跳转到最终的END语句,从而终止程序。

这里,程序是程序(execute block是一个匿名程序)或触发器。

换句话说,exit中的execute block导致execute block终止,仅此而已。

我不知道IBExpert是否支持更高级的脚本选项,但您可以查看从execute block返回一个值并使用脚本中的条件退出(如果可以在IBExpert中使用)。另一个解决方案可能是raise an exception中的execute block(这假设IBExpert在错误时停止脚本)。

© www.soinside.com 2019 - 2024. All rights reserved.