Home » SQL & PL/SQL » SQL & PL/SQL » Using Procedure/Function in a Select Statement (Oracle Database 11g Enterprise Edition Release 11.2.0.4.0)
Using Procedure/Function in a Select Statement [message #642975] Wed, 23 September 2015 21:18 Go to next message
Duane
Messages: 592
Registered: December 2002
Senior Member
Trying to see if this can be done.

What I want to do is run a SELECT statement calling a package/procedure/function that would return a result set.


create or replace procedure get_results (emplid in varchar2,
                                         result out sys_refcursor) is

begin
   open result for
     select *
       from ws_grades
         where student = emplid;
   
end get_results;



SQL Select would be:

select get_results('111') from dual;


Output would be:

A
B
A
C
D
A
B
C


I'm showing a very simple example but the actual package/procedure/function would do a lot more but still return a result set (sys_refcursor) as it's output.

Can that be done?
Re: Using Procedure/Function in a Select Statement [message #642976 is a reply to message #642975] Wed, 23 September 2015 21:41 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>Can that be done?
NOT by your code.

Only a FUNCTION (not a procedure) can be used within SQL statement & it can only return a single object/value
Re: Using Procedure/Function in a Select Statement [message #642977 is a reply to message #642976] Wed, 23 September 2015 21:44 Go to previous messageGo to next message
Duane
Messages: 592
Registered: December 2002
Senior Member
Ok, can it be done at all? If yes, then would the results need to be piped? If no, then there's no way to return a result set in a function. Oracle just can't do that.
Re: Using Procedure/Function in a Select Statement [message #642978 is a reply to message #642977] Wed, 23 September 2015 21:51 Go to previous messageGo to next message
Duane
Messages: 592
Registered: December 2002
Senior Member
This seems to work but the problem I see with this is the result is a "CURSOR". Clicking on that shows the result set I want.


create or replace function get_results (emplid in varchar2)
                                        return sys_refcursor is
                                
   results sys_refcursor;

   begin
     open results for
       select *
         from grades
           where student = emplid;
         
     return results;
   
   end get_results;




select get_results('0100') r from dual




r
[(CURSOR)]




Double Clicking (CURSOR) brings up:


A
B
A
C
D
A
B
C

Re: Using Procedure/Function in a Select Statement [message #642979 is a reply to message #642978] Wed, 23 September 2015 22:24 Go to previous messageGo to next message
John Watson
Messages: 9002
Registered: January 2010
Location: Global Village
Senior Member
For example,
orclz> create or replace type tabnum_t is table of number;
  2  /

Type created.

orclz> create or replace function tabf
  2  return tabnum_t pipelined
  3  as
  4  begin
  5  for i in 1..5 loop
  6  pipe row(i);
  7  end loop;
  8  end;
  9  /

Function created.

orclz> select * from table(tabf);

COLUMN_VALUE
------------
           1
           2
           3
           4
           5

orclz>
Re: Using Procedure/Function in a Select Statement [message #642980 is a reply to message #642978] Wed, 23 September 2015 22:25 Go to previous messageGo to next message
Duane
Messages: 592
Registered: December 2002
Senior Member
I think I got it. Please correct me if there is a better way of doing it.


create or replace type grade_type as object
(
  id    varchar2(20),
  grade varchar2(1)
)




create or replace type grade_table as table of grade_type;






create or replace function get_results (emplid in varchar2)
                                        return grade_table is
                                        
  result grade_table;
 
  begin
  
    select grade_type(a.id, a.grade)
      bulk collect into result
      from (select id, 
                   grade
              from ws_grades
                where id = emplid) a;
    
    return result;
    
   end;





select * from table(get_results('0100'));

Re: Using Procedure/Function in a Select Statement [message #642981 is a reply to message #642979] Wed, 23 September 2015 22:30 Go to previous messageGo to next message
Duane
Messages: 592
Registered: December 2002
Senior Member
Thanks John. I wonder if your way is faster than what I came up with.

By chance, would you know if this would work with a DB Link? That's another requirement where a different campus would be calling this function.


select * from table(tabf)@DBLink;

Re: Using Procedure/Function in a Select Statement [message #642982 is a reply to message #642981] Wed, 23 September 2015 22:35 Go to previous messageGo to next message
John Watson
Messages: 9002
Registered: January 2010
Location: Global Village
Senior Member
You can't pass objects through a link. You'll need to create a view and query that through the link.
I don't know this "by chance". I tested it.

[Updated on: Wed, 23 September 2015 22:36]

Report message to a moderator

Re: Using Procedure/Function in a Select Statement [message #642983 is a reply to message #642982] Wed, 23 September 2015 22:44 Go to previous message
Duane
Messages: 592
Registered: December 2002
Senior Member
Can a VIEW accept an argument? I need to pass the student ID to the function.

Could you show me what this VIEW would look like using your example but your function would take one argument?


CREATE OR REPLACE FORCE VIEW GRADE (ID, GRADE) AS

select * from table(get_results('0100'));




Previous Topic: % with ROWNUM
Next Topic: Procedure call through java
Goto Forum:
  


Current Time: Mon Jun 29 12:19:10 CDT 2026