相当于 Firebird "when any",用于 InterBase 中的 SQL 异常捕获



我需要将用Embarcadero Firemonkey for Windows编写的应用程序扩展到Android,所以我需要InterBase。

此外,目前还没有关于如何在安卓系统下使用Firebird的教程(至少到目前为止还没有(,我发现了一个非常重要的区别,不仅是语言本身,而且是我可以用Firebird和InterBase捕获异常的方式(即插入存储过程(。以下是火鸟代码示例:

ALTER PROCEDURE AD_0_LIST_UPD_ORDER (
AD_0_NAME VARCHAR(20),
AD_0_ORDER INTEGER)
RETURNS (N_ERROR INTEGER)
AS
begin
n_error=0;
begin
update ad_0_list
set ad_0_order = :ad_0_order
where (ad_0_name = :ad_0_name);
when any do begin
n_error=sqlcode;
exit;
end
end
end^

这几行(when any ...(中的美妙之处在于,我有一个捕获任何SQL错误的异常,而在InterBase中,我必须(想象(并为每个过程编写803、-625等情况,理论上从-1到-999。

我找不到翻译它的方法。在InterBase中有这样的方法吗?

InterBase PSQL语言支持WHEN ANY。。。很长一段时间。看见https://docwiki.embarcadero.com/InterBase/2020/en/Handling_Errors和https://docwiki.embarcadero.com/InterBase/2020/en/Examples_of_Error_Behavior_and_Handling

它似乎受到了限制,因为它不将SQLCODE识别为上下文变量,您可以从中获取值,以便在WHEN ANY块中的代码中进行赋值。我同意,这将是一个有用的改进。

作为示例,以下代码在InterBase中工作,尽管不知道生成的确切SQLCODE。但是,您也可以在InterBasePSQL中捕获任何异常。

set echo on; 
/* run some bad statements; see exception code(s) */
/* Expected exception: SQLCODE -413 */
insert into employee (emp_no, first_name, last_name, dept_no, job_code, job_grade, job_country, salary) 
values (2000, 'Two', 'Thousand', 2000, 'SALES', 2000, 'USA', 1); 
/* Expected exception: SQLCODE -297 */
insert into employee (emp_no, first_name, last_name, dept_no, job_code, job_grade, job_country, salary) 
values (2000, 'Two', 'Thousand', 'ABC', 'SALES', 2000, 'USA', 1); 
rollback;
/* Now, do the same with procedures. */
drop procedure TEST_PROC;
COMMIT;
set term ^;
CREATE PROCEDURE TEST_PROC (runcase INTEGER)
RETURNS (N_ERROR INTEGER)
AS
begin
n_error=0;
begin
/* Give bad DEPT_NO value; integral instead of CHAR(3) */
/* Expected exception: SQLCODE -413 */
if (:runcase = 1) then
insert into employee (emp_no, first_name, last_name, dept_no, job_code, job_grade, job_country, salary) 
values (2000, 'Two', 'Thousand', 2000, 'SALES', 2000, 'USA', 1);
/* Give bad SALARY value violating a value constraint */
/* Expected exception: SQLCODE -297 */
if (:runcase = 2) then
insert into employee (emp_no, first_name, last_name, dept_no, job_code, job_grade, job_country, salary) 
values (2000, 'Two', 'Thousand', 'ABC', 'SALES', 2000, 'USA', 1); 
/* good SQL that will succeed; no exception */
if (:runcase = 3) then
INSERT INTO country (country, currency) VALUES ('India', 'Rupee');
/* check for errors */
when any do begin
n_error = :runcase;
exit;
end 
end 
end^
set term ;^
commit;
/* Now test above procedures */
/* see if any work getting done below, by setting count option on */
set count on;
execute procedure test_proc (1);
rollback;
execute procedure test_proc (2);
rollback;
execute procedure test_proc (3);
select * from country where country='India';
rollback;
execute procedure test_proc (99);
rollback;

相关内容

  • 没有找到相关文章

最新更新