• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 900
  • Last Modified:

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

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
vasudev_v
Asked:
vasudev_v
1 Solution
 
dqmqCommented:
A function only supports one return value.  However, a stored procedure supports multiple output parameters.
0
 
OMC2000Commented:
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
 
slightwv (䄆 Netminder) Commented:
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
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
johnsoneSenior Oracle DBACommented:
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
 
slightwv (䄆 Netminder) Commented:
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
 
johnsoneSenior Oracle DBACommented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now