Question

I would like to create an Oracle package accessing an Oracle object of table type.

CREATE OR REPLACE TYPE t_dept_avg_rec AS OBJECT( 
  dept  number, 
  age   number
)

CREATE OR REPLACE TYPE t_dept_avg_tab IS TABLE OF t_dept_avg_rec

Insert few records into the above object type table as mentioned below.

  dept, age
  ----- ---
 - 20,   60
 - 30,   40
 - 20,   24
 - 30,   66

Then create a separate oracle pipelined function to return the following result.

select dept, avg(age)
  from t_dept_avg_tab
group by dept;

Is this possible using Oracle objects?

Was it helpful?

Solution

t_dept_avg_tab is a SQL type so you can use it directly in a FROM with a TABLE() function:

SQL> select dept, avg(age)
  2  from table(t_dept_avg_tab(t_dept_avg_rec(20,60)
  3                         , t_dept_avg_rec(30,40)
  4                         , t_dept_avg_rec(20,24)
  5                         , t_dept_avg_rec(30,66)))
  6  group by dept
  7  /

      DEPT   AVG(AGE)
---------- ----------
        30         53
        20         42

SQL> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top