我有一个函数可以返回类型为my_table%ROWTYPE
的记录,在调用者中,我可以检查返回的记录是否为空,但是PL / SQL会抱怨if语句
PLS-00306:调用'IS NOT NULL'
时参数的数量或类型错误
这是我的代码:
v_record my_table%ROWTYPE;
v_row_id my_table.row_id%TYPE := 123456;
begin
v_record := myfunction(v_row_id)
if (v_record is not null) then
-- do something
end if;
end;
function myfunction(p_row_id in my_table.row_id%TYPE) return my_table%ROWTYPE is
v_record_out my_table%ROWTYPE := null;
begin
select * into v_record_out from my_table
where row_id = p_row_id;
return v_record_out;
end myfunction;
感谢。
答案 0 :(得分:27)
据我所知,这是不可能的。检查PRIMARY KEY
或NOT NULL
列应该足够了。
您可以查看v_record.row_id IS NULL
。
当没有找到记录时,你的函数会抛出NO_DATA_FOUND
异常。
答案 1 :(得分:3)
您无法测试此变量是否存在,因此有两种方法可以实现。检查是否存在单个元素。我不喜欢这个,因为它意味着如果有任何改变你的代码不再有效。相反,为什么不在那里没有数据时引发异常:
我意识到异常中的others
非常顽皮,但只有当它不应该而且没有别的时候它才会真正地消失。
v_record my_table%ROWTYPE;
v_row_id my_table.row_id%TYPE := 123456;
begin
v_record := myfunction(v_row_id)
exception when others then
-- do something
end;
function myfunction(p_row_id in my_table.row_id%TYPE) return my_table%ROWTYPE is
v_record_out my_table%ROWTYPE := null;
cursor c_record_out(c_row_id char) is
select *
from my_table
where row_id = p_row_id;
begin
open c_record_out(p_row_id);
fetch c_record_out into v_record_out;
if c_record_out%NOTFOUND then
raise_application_error(-20001,'no data);
end if;
close c_record_out;
return v_record_out;
end myfunction;