0

In SQL Server, I can just use 'RETURNS TABLE' and it will do the job. But I can't find how to do the same in Oracle SQL

I have the following SELECT statement that needs to be put in a function or procedure:

SELECT a.CodAcord, a.Descr
FROM FreqSoce f
LEFT JOIN Acord a ON a.CodAcord = f.CodAcord
WHERE f.codSoce = codSoce;

codSoce is an INTEGER IN parameter, and I need to return a.CodAcord and a.Descr as result from my function/procedure.
Is there a simple way to do this? Without having to deal with temp variables and/or advanced content...

EDIT: Aditional info:
- I need to return a.CodAcord and a.Descr, but when I did some research to know how to return more than one variable using SQL Functions or Procedures, all I could find was that this was only possible by returning a TABLE. If there's a way to return more than one item from a Function or Procedure, please let me know.
- The use of Functions or Procedures is strictly required.
- I'm using SQL Developer.

2
  • Do you need to return a.CodAcord and a.Descr for a given codScoce, or do you need to return a table? I don't see the connection. Also: do you need a procedure for this, or is the use of bind variables sufficient? What do you use to interface with Oracle: SQL*Plus? Toad? SQL Developer? Each has its means for allowing you to "input" a bind variable and produce the output based on that. Please clarify and we can take it from there. Commented Apr 24, 2016 at 17:13
  • Aditional information provided, thanks for your help. Commented Apr 24, 2016 at 17:20

2 Answers 2

1

You can achieve a table as a return value from function by using a pipelined table function. Please see the example below:

-- Create synthetic case
CREATE TABLE Acord AS
SELECT rownum CodAcord, 'Description ' || rownum Descr
  FROM dual CONNECT BY LEVEL <= 5;

CREATE TABLE FreqSoce AS
SELECT rownum CodSoce, rownum CodAcord
  FROM dual CONNECT BY LEVEL <= 10;

-- Test dataset
SELECT a.CodAcord, a.Descr
  FROM FreqSoce f
  LEFT JOIN Acord a ON a.CodAcord = f.CodAcord
 WHERE f.CodSoce = 10;

-- Here begins actual code
-- Create an object type to hold each table row
CREATE OR REPLACE TYPE typ_acord AS OBJECT(
  CodAcord NUMBER,
  Descr    VARCHAR2(40)
);
/

-- Create a collection type to hold all result set rows
CREATE OR REPLACE TYPE tab_acord AS TABLE OF typ_acord;
/

-- Our function that returns a table
CREATE OR REPLACE FUNCTION getAcord(pCodSoce IN NUMBER)
RETURN tab_acord PIPELINED
AS
BEGIN
  FOR x IN (SELECT a.CodAcord, a.Descr
              FROM FreqSoce f
              LEFT JOIN Acord a ON a.CodAcord = f.CodAcord
             WHERE f.CodSoce = pCodSoce)
  LOOP
    PIPE ROW (typ_acord(x.CodAcord, x.Descr));
  END LOOP;
END;
/

-- Testing the function (please note the TABLE operator)
SELECT * FROM TABLE(getAcord(5));
Sign up to request clarification or add additional context in comments.

1 Comment

Best answer, that I have found so far, to this problem. Thnx
0

Take the following as a code template:

CREATE OR REPLACE PACKAGE tacord AS
    TYPE ttabAcord IS TABLE OF ACord%ROWTYPE;
END tacord;
/
show err

CREATE OR REPLACE PACKAGE BODY tacord AS
BEGIN
    NULL;
END tacord;
/
show err

CREATE OR REPLACE FUNCTION demo RETURN tacord.ttabAcord AS
    to_return   tacord.ttabAcord;
BEGIN
               SELECT a.*
    BULK COLLECT INTO to_return
                 FROM FreqSoce f
            LEFT JOIN Acord a ON a.CodAcord = f.CodAcord
                WHERE f.codSoce = codSoce
                    ;
    RETURN to_return;
END demo;
/
show err

Key points:

  • %ROWTYPE represents the datatype of a database table's record
  • BULK COLLECT INTO inserts a complete result set into a plsql data structure
  • Iteration over the table contents is availabel by the plsql collection methods, namely .FIRST, .LAST, .NEXT.

3 Comments

the code compiles, but executing using SQL Developer returns nothing, is there a special way to execute this?
So what is the common way to execute the procedure?
In the example above, @collapsar uses a FUNCTION. To use a function in PL/SQL context you have to use the value returned, e.g. assign function result to some variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.