tags:

views:

141

answers:

2

How can I construct an object using a query if I also need to insert values in the parent object?

Clearly I'm using the wrong syntax.

EDITED, getting closer:

insert into myTable
select
    mybigtype('foo', 'bar', 'fizzle', myarrayoflittletypes(
        select ref(S)
        from anotherTable S
        where S.stname='dingle'
            or S.stname='fangle')));

mylittletype was created as:

CREATE TYPE myarrayoflittletypes AS VARRAY(20) OF REF mylittletype

anotherTable was created as:

CREATE TABLE anotherTable OF mylittletype

mybigtype has a few strings and a type that's defined to hold an array of references to mylittletype objects. So I want to be able to insert a row into this table that creates an object of type mybigtype with the array constructed based on a query that I define. Can I do this all in one insertion? Or do I need to put in a null placeholder and UPDATE this field subsequently?

Oracle's current complaint is

ORA-01427: single-row subquery returns more than one row

So how can I get the multiple OID returns to this query into the array in the object I'm constructing?

+1  A: 

You almost have it!

insert into myTable
select 
    mybigtype('foo', 'bar', 'fizzle', myarrayoflittletypes(
        (select
            ref(S.mylittletype)
        from anotherTable S
        where S.mylittletype.stname='dingle
            or S.mylittletype.stname='fangle')));
Eric
Thanks much, this gets me most of the way there - this query returns multiple rows, and I want to add them each to the array. Is that possible?
Mitch Flax
I'm a bit confused, as the array should add all rows returned in the subquery to myarrayoflittletypes. What's giving you more than it should?
Eric
(select ref(S) *ERROR at line 5:ORA-01427: single-row subquery returns more than one rowShould I be using a different keyword to indicate that all rows should be added?
Mitch Flax
Also, the outer select statement needs to select from something - does it matter from what I select?
Mitch Flax
How're you handling the array creation, since it's balking at multiple rows being returned? Is myarrayoflittletypes() a user-defined function which parses the results and puts them into a concatenated list?And the outer select just wraps the values up into a select statement so you can have multiple rows if need be.
Eric
I created the array as: CREATE TYPE myarrayoflittletypes AS VARRAY(20) OF REF mylittletype;, and S was created as: CREATE TABLE anotherTable OF mylittletype; - thank you again for the help, Oracle's documentation leaves a lot to be desired.
Mitch Flax
Well, most of my experience is MSSQL and MySQL, so I apologize if I'm a little slow. What does select myarrayoflittletypes(...) get you?
Eric
The inner query yields two rows with single entries of long keys (presumably OID's).
Mitch Flax
A: 

There may be a better way to do this so that I can insert multiple rows into my array from a single query, but the following worked:

insert into myTable
    values(mybigtype('foo', 'bar', 'fizzle', myarrayoflittletypes(
        (select ref(S)
        from anotherTable S
        where S.stname='dingle'),
        (select ref(S)
         from anotherTable S
         where S.stname='fangle'))));
Mitch Flax