我在SQL命令中收到错误未正确结束

时间:2013-06-25 02:54:41

标签: sql oracle11g sqlcommand

当我输入

INSERT INTO works_on
(essn, pno, hours)
values
('123456789', 1, 32.5),
('123456789', 2, 7.5),
('666884444', 3, 40.0),
('453453453', 1, 20.0),
('453453453', 2, 20.0),
('333445555', 2, 10.0),
('333445555', 3, 10.0),
('333445555', 10, 10.0),
('333445555', 20, 10.0),
('999887777', 30, 30.0),
('999887777', 10, 10.0),
('987987987', 10, 35.0),
('987987987', 30, 5.0),
('987654321', 30, 20.0),
('987654321', 20, 15.0),
('888665555', 20, 0);

我收到了以下错误

ORA-00933:SQL命令未正确结束

5 个答案:

答案 0 :(得分:9)

在Oracle中,你不能像这样指定多组值(我猜你是来自允许该语法的MySQL背景)。最简单的方法是生成多个INSERT语句

INSERT INTO works_on
(essn, pno, hours)
values
('123456789', 1, 32.5);

INSERT INTO works_on
(essn, pno, hours)
values
('123456789', 2, 7.5);

INSERT INTO works_on
(essn, pno, hours)
values
('666884444', 3, 40.0);

...

您还可以使用单个INSERT ALL语句插入多行

INSERT ALL
  INTO works_on(essn, pno, hours) values('123456789', 1, 32.5)
  INTO works_on(essn, pno, hours) values('123456789', 2, 7.5)
  INTO works_on(essn, pno, hours) values('666884444', 3, 40.0)
  INTO works_on(essn, pno, hours) values('453453453', 1, 20.0)
  INTO works_on(essn, pno, hours) values('453453453', 2, 20.0)
  INTO works_on(essn, pno, hours) values('333445555', 2, 10.0)
  INTO works_on(essn, pno, hours) values('333445555', 3, 10.0)
  ...
SELECT *
  FROM dual;

答案 1 :(得分:1)

不幸的是,您不能将所有值组合在单个插入中,就像在Oracle中那样。您可以分离SQL语句,也可以使用其他方法在单个语句中运行:

INSERT INTO works_on (essn, pno, hours)
SELECT '123456789', 1, 32.5 FROM DUAL UNION 
SELECT '123456789', 2, 7.5 FROM DUAL UNION 
SELECT '666884444', 3, 40.0 FROM DUAL UNION 
SELECT '453453453', 1, 20.0 FROM DUAL UNION 
...
SELECT '987987987', 30, 5.0 FROM DUAL UNION 
SELECT '987654321', 30, 20.0 FROM DUAL UNION 
SELECT '987654321', 20, 15.0 FROM DUAL UNION 
SELECT '888665555', 20, 0 FROM DUAL

SQL Fiddle Demo

答案 2 :(得分:1)

VALUES语法支持仅将一个条目插入数据库。在ORACLE FAQs中,建议如下:

INSERT ALL
INTO works_on (essn, pno, hours) VALUES ('123456789', 1, 32.5)
INTO works_on (essn, pno, hours) VALUES ('123456789', 2, 7.5)
INTO works_on (essn, pno, hours) VALUES ('666884444', 3, 40.0)
INTO works_on (essn, pno, hours) VALUES ('453453453', 1, 20.0)
INTO works_on (essn, pno, hours) VALUES ('453453453', 2, 20.0)
INTO works_on (essn, pno, hours) VALUES ('333445555', 2, 10.0)
INTO works_on (essn, pno, hours) VALUES ('333445555', 3, 10.0)
INTO works_on (essn, pno, hours) VALUES ('333445555', 10, 10.0)
INTO works_on (essn, pno, hours) VALUES ('333445555', 20, 10.0)
INTO works_on (essn, pno, hours) VALUES ('999887777', 30, 30.0)
INTO works_on (essn, pno, hours) VALUES ('999887777', 10, 10.0)
INTO works_on (essn, pno, hours) VALUES ('987987987', 10, 35.0)
INTO works_on (essn, pno, hours) VALUES ('987987987', 30, 5.0)
INTO works_on (essn, pno, hours) VALUES ('987654321', 30, 20.0)
INTO works_on (essn, pno, hours) VALUES ('987654321', 20, 15.0)
INTO works_on (essn, pno, hours) VALUES ('888665555', 20, 0)
SELECT * FROM dual;

答案 3 :(得分:0)

试着看看ShoeLace的this

Oracle SQL uses a semi-colon ;  as its end of statement marker.

you will need to add the ; after bother insert statments.

NB: that also assumes ADODB will allow 2 inserts in a single call.

the alternative might be to wrap both calls in a block,

    BEGIN
          insert (...) into (...);
          insert (...) into (...);
    END;

答案 4 :(得分:-3)

INSERT INTO 
WORKS_ON VALUES
(&ESSN,&PNO,&HOURS);

你会得到以下数据。

ENTER VALUE FOR ESSN: IN THIS U ENTER 123456789

ENTER VALUE FOR PNO: IN THIS U ENTER 1

ENTER VALUE FOR HOURS:32.5

之后你会得到:

  

1行创建

然后输入'/'符号并按Enter键。您将获得输入以在该表中插入另一行。

按照上述步骤,可以轻松地将行插入表格。