【问题标题】:INSERT SELECT statement in Oracle 11GOracle 11G 中的 INSERT SELECT 语句
【发布时间】:2011-11-11 12:16:24
【问题描述】:

我正在尝试在 Oracle 11g 中运行一个非常简单的 sql 语句。

 insert into table1 (col1, col2) values (select t1.col1, t2.col2 from oldtable1 t1, oldtable2 t2);

非常简单的查询。笛卡尔将旧表 1 连接到旧表 2,将结果值放入表 1。

我自己运行了子查询,它运行良好。

 select t1.col1, t2.col2 from oldtable1 t1, oldtable2 t2

当我尝试运行完整的语句时,我收到以下错误:

 SQL Error: ORA-00936: missing expression
 00936. 00000 -  "missing expression"

我也不能让它在 MySql 中工作。我的陈述有问题,但我不确定是什么。

【问题讨论】:

标签: sql oracle oracle11g


【解决方案1】:

您的查询应该是:

insert into table1 (col1, col2) 
select t1.col1, t2.col2 
from oldtable1 t1, oldtable2 t2

即没有VALUES 部分。

【讨论】:

    【解决方案2】:

    去掉values 关键字和括号。你可以看一个例子here

    这是基本的 INSERT 语法:

    INSERT INTO "table_name" ("column1", "column2", ...)
    VALUES ("value1", "value2", ...);
    

    这是 INSERT SELECT 语法:

    INSERT INTO "table1" ("column1", "column2", ...)
    SELECT "column3", "column4", ...
    FROM "table2";
    

    【讨论】:

    • 我认为VALUES子句中的项目需要用单引号引起来
    【解决方案3】:

    使用“选择”作为来源时,不需要“值”子句。

    insert into table1 (col1, col2) 
    select t1.col1, t2.col2 from oldtable1 t1, oldtable2 t2;
    

    【讨论】:

      【解决方案4】:

      用于将数据插入表中,您可以编写

      insert into tablename values(column_name1,column_name2,column_name3);
      

      但是按照表中的顺序在顺序中写入column_name ...

      【讨论】:

        【解决方案5】:

        还有另一个选项可以将数据插入表中..

        insert into tablename values(&column_name1,&column_name2,&column_name3);
        

        它将打开另一个窗口用于插入数据值..

        【讨论】:

          猜你喜欢
          • 2019-09-02
          • 2014-11-27
          • 2011-12-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多