将多个表中的行添加到新表sql中的麻烦

时间:2013-10-25 19:03:01

标签: sql oracle toad

我正在使用toad for oracle,我想从多个表中获取数据并将它们插入到新表中。

这是我的剧本:

insert into mydb.testTable2 (FAO, Company, Cost_Center, Description)
select (select FAO from bigdb.wtags),
       (select DESCRIPTOR from bigdb.wtags),
       (select cost_center from bigdb.MASTERFILE),
       ''
from bigdb.wtags join bigdb.masterfile on bigdb.wtags.fao = bigdb.MASTERFILE.workday_number

我收到错误:

table or view does not exist

我一直在www.w3schools.com上阅读sql,它说insert into语句用于创建新表。 我不能使用多个数据源创建新表吗?

另外,我可以将从同一个表中获取的2个select语句组合成1行吗? 我尝试了但它给了我一个错误:too many valuesmissing expression。 这可能是由于另一个错误吗?

1 个答案:

答案 0 :(得分:1)

table or view does not exist表示您在SQL中指定的表或视图在数据库中不存在(或者它存在,但您无权访问它)。

这意味着以下其中一项不存在:

  • mydb.testTable2
  • bigdb.wtags
  • bigdb.MASTERFILE

除此之外,您的查询结构并没有多大意义。在编写时,您正在查询bigdb.wtagsbigdb.masterfile,但不使用任何结果 - 而是尝试从其他三个单独的查询中插入。我怀疑你正在尝试做类似的事情:

insert into mydb.testTable2 (FAO, Company, Cost_Center, Description)
select bigdb.wtags.FAO,
       bigdb.wtags.DESCRIPTOR,
       bigdb.MASTERFILE.cost_center,
       null
  from bigdb.wtags 
       join bigdb.masterfile 
           on bigdb.wtags.fao = bigdb.MASTERFILE.workday_number

如果您希望创建表作为插入的一部分,则语法略有不同:

create table mydb.testTable2 as
select bigdb.wtags.FAO,
       bigdb.wtags.DESCRIPTOR,
       bigdb.MASTERFILE.cost_center,
       null description
  from bigdb.wtags 
       join bigdb.masterfile 
           on bigdb.wtags.fao = bigdb.MASTERFILE.workday_number