patternMinor
What could be the reason for disallowing a sub query in the values clause?
Viewed 0 times
thewhatquerysubcouldreasonforvaluesclausedisallowing
Problem
For example
SQL> create table dates(d date);
Table created.
SQL> insert into dates select sysdate from dual;
1 row created.
SQL> select * from dates;
D
---------
28-MAY-11
SQL> insert into dates (d) values (select sysdate fom dual);
insert into dates (d) values (select sysdate fom dual)
*
ERROR at line 1:
ORA-00936: missing expression
SQL>Solution
If you want to use a SELECT statement where only a single value is allowed you need to put that SELECT statement into brackets:
That could be extended for multiple columns:
You just need to make sure that the SELECT returns exactly one row and exactly one column
insert into dates (d)
values
( (select sysdate from dual) )That could be extended for multiple columns:
insert into dates
(
id,
d,
other_column
)
values
(
some_sequence.nextval,
(select sysdate from dual),
(select max(some_col) from other_table)
)You just need to make sure that the SELECT returns exactly one row and exactly one column
Code Snippets
insert into dates (d)
values
( (select sysdate from dual) )insert into dates
(
id,
d,
other_column
)
values
(
some_sequence.nextval,
(select sysdate from dual),
(select max(some_col) from other_table)
)Context
StackExchange Database Administrators Q#3012, answer score: 7
Revisions (0)
No revisions yet.