ORA-00936缺少表达

时间:2012-11-02 05:54:43

标签: sql oracle error-handling

以下声明:

INSERT INTO TABLE1(COL_1,COL2) VALUES(SELECT MAX(COL_1) FROM TABLE1), 'XYZ');

抛出错误:

ERROR at line 1:
ORA-00936: missing expression
 at the select clause. 
1. The table is empty for now.
2. COL_1 is a primary key intger field.

你能帮我吗?

3 个答案:

答案 0 :(得分:3)

尝试

    create table TABLE1 (COL_1 number, COL2 varchar2(5));

ALTER TABLE TABLE1
add CONSTRAINT t_pk PRIMARY KEY (col_1);

INSERT INTO TABLE1(COL_1,COL2) VALUES((SELECT nvl(MAX(COL_1),0) FROM TABLE1), 'XYZ');

INSERT INTO TABLE1(COL_1,COL2) VALUES((SELECT nvl(MAX(COL_1+1),0) FROM TABLE1), 'XYZ');

SQL Fiddle Demo

答案 1 :(得分:2)

如果要将查询结果用作标量表达式,请将整个(子)查询括在括号中,如下所示:

INSERT INTO TABLE1(COL_1,COL2) VALUES (
   (SELECT MAX(COL_1) FROM TABLE1),
  'XYZ'
);

另一方面,你可以在这里使用different syntax

INSERT INTO TABLE1(COL_1,COL2)
SELECT MAX(COL_1), 'XYZ' 
FROM TABLE1
group by  'XYZ';

答案 2 :(得分:0)

删除一个额外的括号

INSERT INTO TABLE1(COL_1,COL2) SELECT MAX(COL_1) , 'XYZ' FROM TABLE1