Solved

returning different datatype from a function Oracle PL/SQL

Posted on 2014-03-15
8
483 Views
Last Modified: 2014-03-20
create FUNCTION myfunc
return VARCHAR2
is
v_ret NUMBER;
BEGIN
-- functionality which sets v_ret to -1 or (1/2/4/8)
return v_ret;
END myfunc;

My question is this v_ret will be converted to VARCHAR2 ? Will this create any issues in runtime.?
0
Comment
Question by:priyanka999kamlekar
  • 3
  • 2
8 Comments
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 333 total points
ID: 39931027
it should run and return ok.

problems would only arise if the invoking code tried to do something with the value assuming it was something other than a varchar2.
0
 

Author Comment

by:priyanka999kamlekar
ID: 39931127
Do you mean this?
i am calling this function myfunc in another function and storing the returned value in the NUMBER variable.

v_ret NUMBER;
v_ret := myfunc();

In this case what will happen?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 167 total points
ID: 39931283
this will work, as long as the value returned is indeed a "number" value,
given the current code, it shall work, but the "interface declaration" of the function, returning a varchar, it might indeed some day return a non-number function.

so, the best way is either to change the function to RETURN NUMBER instead of RETURN VARCHAR2, or do assume first the function returns a varchar2, and test if the value returned is really a number (or not) before trying to cast/convert it to a number
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 333 total points
ID: 39931631
If you return the string '-1'  to v_ret  then v_ret will get an implicit conversion of '-1' the string to -1 the number.

If you return the string '(1/2/4/8)'  then the implicit conversion will be attempted again but it will fail because that is not a number
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39933997
A penalty grade is not appropriate here, I've requested moderator review
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39941904
split among the 3 expert comments
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question