Home > other >  Can I use a select statement for inserting values with an object type?
Can I use a select statement for inserting values with an object type?

Time:03-13

Say I have this object

create or replace type emp_det as object (
    age number,
    salary number
    )
/

and this table

create table temp(
    id number,
    emp_obj emp_det
)
/

And a populated third table: employees

| Age | Salary |
| 45  | 75000  |
| 23  | 32000  |
...

I want to insert into temp table with something like:

insert into temp
values (1, emp_det( (select age,salary from employees where ROWNUM = 1) ));/

but I get an error "incorrect number of arguments for default constructor". Is there a way to do this with an insert statement?

(I know I dont need objects for this, Its just a quick example)

CodePudding user response:

"Something like" should look like this:

SQL> insert into temp (id, emp_obj)
  2    select 1, emp_det(age, salary)
  3    from employees
  4    where rownum = 1;

1 row created.

SQL> select * from temp;

        ID    EMP_OBJ(AGE, SALARY)
----------    -----------------------------------------
         1    EMP_DET(45, 75000)


SQL>
  • Related