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

Can I programmatically exit a Firebird script

考虑以下脚本:

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 自己处理?有条件地退出整个脚本有不同的方法吗?

您混淆了在脚本中使用 exit 和在 Firebird 语句(特别是 execute block)中使用 exit。 IBExpert 将脚本中的普通 exit 解释为停止脚本的信号。

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

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

The EXIT statement causes execution of the procedure or trigger to jump to the final END statement from any point in the code, thus terminating the program.

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

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

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