Solved

returning different datatype from a function Oracle PL/SQL

Posted on 2014-03-15
8
485 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and theā€¦
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

732 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