Return boolean value from oracle function

此生再无相见时 提交于 2021-02-04 17:30:06

问题


Trying to return value from function

create or replace function compairenumber(num1 in number,num2 in number)
return boolean is
begin
if num1 < num2 then
return true;
else 
return false;
end if;
end;

when i'm giving query select compairenumber(5,10) from dual its not returning true or false.


回答1:


Boolean values can only be used in other PL/SQL code, not in Oracle SQL. If you want a function whose return value is available in a select ... from dual then you will need to define the function to return varchar2 with the return values 'true' and 'false' respectively (or 'T' and 'F', or return number, with the values 1 and 0).

As sad as this is, Oracle SQL does not support the Boolean data type (although the programming language PL/SQL does).




回答2:


use return varchar2

create or replace function compairenumber(num1 in number, num2 in number)
  return varchar2 is
begin
  if num1 < num2 then
    return 'TRUE';
  else
    return 'FALSE';
  end if;
end;


select CASE
         WHEN compairenumber(5, 10) = 'TRUE' THEN
          'OK'
         ELSE
          'NOT'
       END
  from dual


来源:https://stackoverflow.com/questions/41799473/return-boolean-value-from-oracle-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!