以下函数检查名为ZIPCODE的表中是否存在邮政编码:
create or replace function zip_does_not_exist(i_zip in zipcode.zip%type)
return boolean
as
v_zip zipcode.zip%type;
begin
select zip into v_zip
from zipcode
where zip = i_zip;
if v_zip is not null then
return false;
else
return true;
end if;
end;
/
Run Code Online (Sandbox Code Playgroud)
为了测试这个函数,我发出了以下语句,该语句应该返回false,因为作为参数传递的邮政编码存在于ZIPCODE表中:
select zip_does_not_exist('00914') from dual;
Run Code Online (Sandbox Code Playgroud)
但是,我在尝试运行此代码时收到此消息:
Error report:
SQL Error: ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
*Cause:
*Action:
Run Code Online (Sandbox Code Playgroud)
这不应该发生,因为'00914'是作为参数所需的正确数据类型(varchar2(5)).我为什么收到这条消息?
试着把它称为:
create or replace function zip_does_not_exist(i_zip in zipcode.zip%type)
return pls_integer
as
v_zip zipcode.zip%type;
begin
select zip into v_zip
from zipcode
where zip = i_zip;
if v_zip is not null then
return 0;
else
return 1;
end if;
end;
/
Run Code Online (Sandbox Code Playgroud)
然后你会成功打电话:
select zip_does_not_exist('00914') from dual;
Run Code Online (Sandbox Code Playgroud)
因为,不能将boolean设置为sql语句的返回变量.