Solved

returning different datatype from a function Oracle PL/SQL

Posted on 2014-03-15
8
475 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 73

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 142

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 73

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 73

Expert Comment

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

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.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now