1

I want to create a constructor for the 2 types below. Here's the code:

create or replace type toys_t as table of varchar2(40);

create or replace type kid_t as object (
      name varchar2(10), 
      toys toys_t,
      constructor function kid_t (name varchar2) return self as result);

create table kid of kid_t nested table toys store as table_toys;

Is there a way of creating a user-defined constructor for the nested table type toys_t or is it only supported for TYPEs created using the as object syntax?

Thanks

Bob

APC
  • 144,005
  • 19
  • 170
  • 281
Bob darkside
  • 13
  • 1
  • 5

2 Answers2

2

There is automatically a constructor for collections like toys_t. Just use the name of the collection

SQL> insert into kid values( 'Bobby', toys_t( 'Bike', 'Ball', 'Legos' ));

1 row created.

The toys_t constructor can take 0, 1, or many parameters.

Justin Cave
  • 227,342
  • 24
  • 367
  • 384
  • But can't we define one like in my example where there's only one parameter? Thanks – Bob darkside Jun 16 '12 at 16:16
  • @Bob - The implicit constructor for the `toys_t` type can take 0, 1, or multiple parameters. I'm not sure what benefit you would expect to gain by defining your own. Can you explain what you would want to do that the implicit constructor doesn't do? – Justin Cave Jun 16 '12 at 16:36
2

No, we cannot declare user-defined constructors for types like your toys_t table. Collection types (nested tables or varrays) only have default constructors. All we can do is specify a default which is applied when we instantiate a collection without any arguments. Find out more.

APC
  • 144,005
  • 19
  • 170
  • 281