What could be the reason for disallowing a sub query in the values clause?
-
16-10-2019 - |
سؤال
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>
المحلول
If you want to use a SELECT statement where only a single value is allowed you need to put that SELECT statement into brackets:
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
نصائح أخرى
I think without knowing (I don't do Oracle so don't have a box to try this on) that the problem is here:
insert into dates (d) values (select sysdate fom dual)
^^^^^^ <- this isn't a date; it expects a date here
Try this instead:
insert into dates (d)
select sysdate fom dual
Because the parenthesis tells the grammar that the value inside the paren is a value, not a value producing statement. I suppose, if you really want a more indepth explanation that I could go and look that up for you in the SQL language spec.
I guess it is just a historic fact.
It is just not yet done that way.
From SQL Server I know that they are releasing such restrictions between 2005 and 2008 and we often haven step into problems when we run such scripts on 2005.