Solved

Return 2 data values (integer, varchar2) from Oracle stored Function

Posted on 2007-03-28
6
864 Views
Last Modified: 2010-08-05
I'm new developer in Oracle. So please bear if it's too basic question and would appreciate your patience in answering.   My requirement is to get 2 parameter (Integer,varchar2)values back from Oracle Stored Function. Could someone give me an example on how to accomplish this.
      
0
Comment
Question by:vasudev_v
6 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 18809493
A function only supports one return value.  However, a stored procedure supports multiple output parameters.
0
 
LVL 15

Accepted Solution

by:
OMC2000 earned 50 total points
ID: 18809577
You could convert your integer value to string and concatenate it with your string with some seprater character,  '#' for instance, then in caller procedure or function you could extract both values with instr and substr builtin functions
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 18809606
You can return a delimited character string something like:
----------------------
create or replace function myFunc return varchar2
is
      someNumber number := 1;
      someString varchar2(20) := 'some string';
begin
      return to_char(someNumber) || ':' || someString;
end;
/

show errors

select myFunc() from dual;



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 34

Expert Comment

by:johnsone
ID: 18810824
Just to clarify, out parameters are supported in both functions and procedures.

create function test_func (out_var out varchar2) return number as
begin
out_var := 'test';
return(1);
end;
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 18811464
True but you can't use a function with an out parameter as part of a select statement.  You have to use it within PL/SQL itself:
----------------------------
var bob varchar2(100)

-- fails
select test_func(:bob) from dual;

--works
declare
      myString varchar2(10);
      myNumber number;
begin
      myNumber := test_func(myString);
      dbms_output.put_line('String: ' || myString);
      dbms_output.put_line('Number: ' || myNumber);
end;
/
0
 
LVL 34

Expert Comment

by:johnsone
ID: 18815518
Not sure of the original intention for use.  Just wanted to point out that it is possible to use out parameters in a function.  May not be practical in this case.

As a more complex way to return more than one value, a ref cursor is possible and can be used in a select statement.

create or replace package test_pack is
 type rcur is ref cursor;
 function test_func return rcur;
end;
/

create or replace package body test_pack is
 function test_func return rcur is
   return_cursor rcur;
 begin
   open return_cursor for select 1, 'test' from dual;
   return return_cursor;
 end;
end;
/
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

Suggested Solutions

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows how to recover a database from a user managed backup
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

708 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

16 Experts available now in Live!

Get 1:1 Help Now