对象类型变量可以是NULL. 用这条线来捕捉这种情况非常简单:
if v_obj is null then ...
但是,如果变量本身已初始化,但绝对每个属性都包含值NULL怎么办?使用数组,一切都很简单。我们可以抓住这一刻:
if v_arr is null or v_arr is empty then ...
有没有类似的方法来理解一个对象的所有属性都包含一个空值?
例如:
create or replace type t_obj as object (
a number,
b number,
c number,
constructor function t_obj return self as result);
/
create or replace type body t_obj as
constructor function t_obj return self as result is
begin
return;
end;
end;
/
declare
v_obj t_obj;
begin
if v_obj is null /* or ??? */
then dbms_output.put_line('v_obj is null');
else dbms_output.put_line('v_obj is not null');
end if;
v_obj := t_obj();
if v_obj is null /* or ??? */
then dbms_output.put_line('v_obj is null');
else dbms_output.put_line('v_obj is not null');
end if;
v_obj.a := 5;
if v_obj is null /* or ??? */
then dbms_output.put_line('v_obj is null');
else dbms_output.put_line('v_obj is not null');
end if;
end;
结果,在控制台中我们将看到:
v_obj is null
v_obj is not null
v_obj is not null
我想看看(在替换 ??? 并打开评论之后)
v_obj is null
v_obj is null
v_obj is not null
作为一个选项,我看到:将MAP MEMBER FUNCTION对象类型添加到正文和if v_obj = t_obj(). 但是,不幸的是,无法访问类型代码(由生成器提供)。
没有检查所有属性的内置函数
NULL。如果无法将自己的函数添加到类型中,那么最简单的方法是将验证逻辑显式添加到代码中:
当然,您可以使动态查询更加舒适,但要以性能损失为代价。为每种类型创建一个重载函数:
问题中的示例块将返回预期结果: