插入select from子句导致'无法插入多行错误'

时间:2014-12-12 18:25:20

标签: sql oracle select insert

我使用insert into select from子句将数据插入表中,但它不起作用。

请看这里:

insert into STAGES_ROLES_REL
              select 
                  (select stage_id 
                   from
                  (select ws2.workflow_stage_id as stage_id
                         from
                        ( select ws.workflow_stage_name as workflow_stage_name
                          from   workflow_stages ws
                          join   stages_roles_rel sr
                            on   ws.workflow_stage_id = sr.workflow_stage_id ) table1
                  join workflow_stages ws2    
                  on   table1.workflow_stage_name =  ws2.workflow_stage_name
                  order by ws2.workflow_stage_id))
             ,role_id
             ,current_date created_date, user created_by
             ,null modified_date, null modified_by
              from stages_roles_rel

错误是无法插入多行,但我不知道如何设置过滤器并获取单个行。

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

我的猜测是这个查询返回了多个结果:

select stage_id
    from (
      select ws2.workflow_stage_id as stage_id
      from (
        select ws.workflow_stage_name as workflow_stage_name
        from workflow_stages AS ws
        inner join stages_roles_rel AS sr
          on ws.workflow_stage_id = sr.workflow_stage_id
        ) AS table1
      inner join workflow_stages ws2    
      on table1.workflow_stage_name =  ws2.workflow_stage_name
      order by ws2.workflow_stage_id
    )

如果这是您想要的(该查询中有多行),请将常量移动到该查询中并删除外部查询:

insert into STAGES_ROLES_REL
select
  stage_id,
  role_id,
  current_date AS created_date,
  user AS created_by,
  null AS modified_date,
  null AS modified_by
from (
  select ws2.workflow_stage_id as stage_id
  from (
    select ws.workflow_stage_name as workflow_stage_name
    from workflow_stages AS ws
    inner join stages_roles_rel AS sr
      on ws.workflow_stage_id = sr.workflow_stage_id
    ) AS table1
  inner join workflow_stages ws2    
  on table1.workflow_stage_name =  ws2.workflow_stage_name
  order by ws2.workflow_stage_id
    )
   [add a cross join to the original table if that's what you're wanting, I don't know the intent of your original query]

否则,您需要修复查询以仅返回一行。