Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an object type in a select statement in Oracle

Tags:

sql

oracle

plsql

I have a function that returns an object that has 3 values. Is there a way to call that function from a select statement and have each value be a different column? I could break it into 3 functions, but the values are related so I wanted to keep it as one for performance reasons. (So oracle doesn't have to call 3 very similar complex functions for every row in the query.)

So for:

create type test_obj is object ( 
 a   NUMBER, 
 b   NUMBER,
 c   NUMBER);

    create or replace function test_func (
     pinput   NUMBER)
     return test_obj
    as
    begin
     return test_obj(0, 0, 0);
    end test_func;

I'd like to be able to call test_func from a select statement, but have a, b, and c be different columns, without calling the function multiple times. I thought maybe something like this, but it doesn't work:

select
    iv.col1,
    iv.col2,
    iv.func_data.a,
    iv.func_data.b,
    iv.func_data.c
from
    (select
        mt.col1,
        mt.col2,
        test_func(mt.input) as func_data
    from
        my_table mt) iv

Is there a way to do anything like this in Oracle 10g, or is there a better way to solve this problem?

like image 796
Chris B Avatar asked Oct 19 '09 16:10

Chris B


People also ask

What is the use of object type in Oracle?

Oracle object types are user-defined types that make it possible to model real-world entities, such as customers and purchase orders, as objects in the database. New object types can be created from any built-in database types and any previously created object types, object references, and collection types.

How do you declare an object type in Oracle PL SQL?

Currently, you cannot define object types in a PL/SQL block, subprogram, or package. You can define them interactively in SQL*Plus using the SQL statement CREATE TYPE. After an object type is defined and installed in the schema, you can use it to declare objects in any PL/SQL block, a subprogram, or package.

Can we use case in SELECT statement in Oracle?

Introduction to Oracle CASE expression You can use a CASE expression in any statement or clause that accepts a valid expression. For example, you can use the CASE expression in statements such as SELECT , UPDATE , or DELETE , and in clauses like SELECT , WHERE , HAVING , and ORDDER BY .

What is the difference between object type and record type in Oracle?

Answers. Object is SQL type. Record is an PL/SQL type. SET SERVEROUTPUT ON DECLARE TYPE rec_empdet IS RECORD(empno NUMBER(10), ename VARCHAR2(20), deptno NUMBER(10), dname VARCHAR2(20) ); v_empdet rec_empdet; BEGIN SELECT empno,ename,e.


2 Answers

The select statement in the question will work. It was failing because I didn't include an alias for the inline view.

For some reason this will work:

select
    iv.func_data.a,
    iv.func_data.b,
    iv.func_data.c
from
    (select
        test_func(mt.input) as func_data
    from
        my_table mt) iv

But this won't:

select
    func_data.a,
    func_data.b,
    func_data.c
from
    (select
        test_func(mt.input) as func_data
    from
        my_table mt)
like image 94
Chris B Avatar answered Oct 20 '22 15:10

Chris B


The table alias places the result into a named result set context, which enables the result to work as an object instance. Without the alias, it fails because the casting doesn't handle object type instances without their own explicit reference, which is effectively what the table alias is.

like image 2
Michael Avatar answered Oct 20 '22 13:10

Michael