Solved

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

Posted on 2007-03-28
6
871 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
automatic email alert 1 52
ORA-06504: PL/SQL: Return types of Result Set variables or query do not match 4 112
Select and Insert Query running slow 4 36
use lov values 2 50
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

809 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