문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 dba.stackexchange
scroll top