views:

141

answers:

1

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

+2  A: 

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.

Adam Musch
+1 for pointing out this is a bad way to design a table.
Tony Andrews