Question

I have the following statements in Oracle 11g:

CREATE TYPE person AS OBJECT (
    name VARCHAR2(10),
    age NUMBER
);

CREATE TYPE person_varray AS VARRAY(5) OF person;

CREATE TABLE people (
    somePeople person_varray
)

How can i select the name value for a person i.e.

SELECT somePeople(person(name)) FROM people

Thanks

Was it helpful?

Solution

I'm pretty sure that:

  1. What you're doing isn't what I'd be doing. It sort of completely violates relational principles, and you're going to end up with an object/type system in Oracle that you might not be able to change once it's been laid down. The best use I've seen for SQL TYPEs (not PL/SQL types) is basically being able to cast a ref cursor back for pipelined functions.

  2. You have to unnest the collection before you can query it relationally, like so:

    SELECT NAME FROM (SELECT SP.* FROM PEOPLE P, TABLE(P.SOME_PEOPLE) SP)

That'll give you all rows, because there's nothing in your specifications (like a PERSON_ID attribute) to restrict the rows.

The Oracle Application Developer's Guide - Object Relational Features discusses all of this in much greater depth, with examples.

OTHER TIPS

To insert query:-

insert into people values (
person_varray(person('Ram','24'))
);

To select :-

select * from people;

SELECT NAME FROM (SELECT SP.* FROM PEOPLE P, TABLE(P.somePeople) SP)

While inserting a row into people table use constructor of 
person_varray and then the constructor 
of person type for each project.
The above INSERT command 
creates a single row in people table.

 select somePeople from people ;

 person(NAME, age) 
---------------------------------------------------
person_varray(person('Ram', 1), 

To update the  query will be:-

update people 
set somePeople =  
           person_varray 
           (  
               person('SaAM','23')  
           ) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top