我正在使用sql开发人员,并已添加约束到我的一个表。在psql中捕获约束违规
constraint valid_gender check(gender in ('M','F','I','T'))
当我尝试使用plsql过程添加性别为'x'的条目时,它违反约束(因为它应该)失败。
我想添加一个“Catch”到plsql过程,这样如果valid_gender被声明,我可以raise_application_error特定于它。这可能吗?
我正在使用sql开发人员,并已添加约束到我的一个表。在psql中捕获约束违规
constraint valid_gender check(gender in ('M','F','I','T'))
当我尝试使用plsql过程添加性别为'x'的条目时,它违反约束(因为它应该)失败。
我想添加一个“Catch”到plsql过程,这样如果valid_gender被声明,我可以raise_application_error特定于它。这可能吗?
甲骨文将提高,上面写着一个例外:
ORA-02290:检查约束(yourschema.valid_gender)违反
你能赶上,在异常处理程序,提高自己的异常,而不是通过几种方式使用raise_application_error
。
1)您可以专门捕获ORA-02290的例外是这样的:
declare
e_check_violated exception
pragma exception_init (e_check_violated, -2290);
begin
insert ...
exception
when e_check_violated then
if sqlerrm like '%(yourschema.valid_gender)%' then
raise_application_error(-20001,'Invalid gender');
else
raise;
end if;
end;
2)可以捕获所有异常,并检查它们:
begin
insert ...
exception
when others then
if sqlerrm like 'ORA-02290:%(yourschema.valid_gender)%' then
raise_application_error(-20001,'Invalid gender');
else
raise;
end if;
end;
在大型应用还是比较通常有一个异常处理程序来概括这一点,并在表中查找特定于约束的消息。
你可能只是第一个测试:
if gender_value not in ('M','F','I','T') then
raise_application_error...
end if;
使用化名块在你的代码...
BEGIN
INSERT or update...
EXCEPTION
WHEN dup_val_on_index THEN
RISE...
END;
三江源非常 – luke
我会+2或+3,如果我能:) –
这是一个比较合适的答案,谢谢 – chulian