插入多行查询失败

时间:2019-09-05 06:28:54

标签: sql oracle ora-00933

INSERT INTO EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)
VALUES  (7499,'ALLEN','SALESMAN',7698,'20-Feb-81',1600,300,30)
      , (7521,'WARD','SALESMAN',7698,'22-Feb-81',1250,500,30);

试图同时插入两行。但未能说“ SQL命令未正确结束”。有人可以更正查询吗?

错误:

  

命令行错误:18列:125错误报告-SQL错误:ORA-00933:SQL命令未正确终止00933。00000-“ SQL命令未正确终止” *原因:*操作:

4 个答案:

答案 0 :(得分:3)

根据您的错误消息(ORA-00933:SQL命令未正确结束),DBMS是Oracle。

您可以对Oracle中的INSERT INTO使用以下查询。

INSERT ALL  
  INTO EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES (7499,'ALLEN','SALESMAN',7698,'20-Feb-81',1600,300,30)  
  INTO EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES (7521,'WARD','SALESMAN',7698,'22-Feb-81',1250,500,30)

答案 1 :(得分:2)

尝试如下

insert into emp  (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)

select 7499,'ALLEN','SALESMAN',7698,'20-Feb-81',1600,300,30  from dual
union all
select 7521,'WARD','SALESMAN',7698,'22-Feb-81',1250,500,30 from dual

答案 2 :(得分:1)

要在ORACLE中插入多个记录,您需要将记录合并到cte中。或使用@Arulkumar提到的Insert All

INSERT INTO Emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) 
  WITH names AS ( 
    SELECT 7499,'ALLEN','SALESMAN',7698,'20-Feb-81',1600,300,30 UNION ALL 
    SELECT 7521,'WARD','SALESMAN',7698,'22-Feb-81',1250,500,30 
  ) 
  SELECT * FROM names

您可以在Oracle中找到此链接以获取有关插入命令的更多信息。Link

答案 3 :(得分:1)

您的语法适用于Microsoft SQL Server,但您的错误消息来自Oracle DBMS。

您可以使用INSERT ALL查询:

INSERT ALL
  INTO mytable (column1, column2, column_n) VALUES (expr1, expr2, expr_n)
  INTO mytable (column1, column2, column_n) VALUES (expr1, expr2, expr_n)
  INTO mytable (column1, column2, column_n) VALUES (expr1, expr2, expr_n)
SELECT * FROM dual;

请参见Official Oracle Documentation