PL/SQL function returning Boolean
in PL/SQL stored function can return boolean values tool. but it should be checked in IF or CASE statements only.
Here is the Example: Function checks if record exists or not
if exists return true else false.
create or replace function custExists(p_custid in nchar)
return boolean
as
recfound number(2);
begin
select count(*) into recfound from customers where customerid=p_custid;
DBMS_OUTPUT.PUT_LINE(P_CUSTID||' '||RECFOUND);
if( recfound >=1) then
return true;
ELSE
return false;
return false;
end if;
exception
when others then
begin
dbms_output.put_line('no record found');
return false;
end;
end;
Calling/Consuming Function returning boolean
SQL>begin
if( custExists('ABCDE')) then
dbms_output.put_line('record exists');
else dbms_output.put_line('record not exists');
end if;
end;
if( custExists('ABCDE')) then
dbms_output.put_line('record exists');
else dbms_output.put_line('record not exists');
end if;
end;
--execute the above code.
SQL>/
ABCDE 1
record exists
PL/SQL procedure successfully completed.
record exists
PL/SQL procedure successfully completed.
Note: User can not consume/call in SELECT statement.
Tags:PL/SQL function returning boolean, Oracle function returning Boolean value, Executing boolean returned function in PL/SQL block, PL/SQL anonymous block.
whow
ReplyDelete