Oracle, insert multirows from subquery with more than one row

三世轮回 提交于 2019-12-07 19:18:16

问题


Im trying to copy some field from a table to other, I want to do iy by using insert with a subquery like this:

insert into sed_reporte_generico 
(srg_usuario, 
srg_nombres,
srg_ape_paterno,
srg_ape_materno,
srg_objetivo,
srg_peso_ob,
srg_calf_ob)
values
(
(select us.su_st_usuario, us.su_st_nombres, us.su_st_ap_paterno, us.su_st_ap_materno,     ob.soc_st_descripcion, ob.soc_nr_peso,ob.soc_nr_calificacion 
from sed_objetivo ob, sed_usuarios us, sed_evaluacion ev 
where ob.se_evaluacion_pk = ev.se_evaluacion_pk and ev.su_colaborador_fk =     us.su_usuarios_pk)
);

but I got this error:

01427. 00000 -  "single-row subquery returns more than one row"

any idea how should I do this?

Thanks,


回答1:


Think you got to choose between

insert into table (a, b, c) VALUES(1, 2, 3)

and

insert into table (a, b, c) 
(SELECT x, y, z from table2)

You can have VALUES and SELECT mixed only (as pointed by an anomyous horse) when your select query returns only one column and one row !

By the way, use JOIN... to Join your tables (use of the WHERE clauses to join tables is rather a bad habit) :

INSERT INTO sed_reporte_generico 
(srg_usuario, 
srg_nombres,
srg_ape_paterno,
srg_ape_materno,
srg_objetivo,
srg_peso_ob,
srg_calf_ob)

(select us.su_st_usuario, us.su_st_nombres, us.su_st_ap_paterno, us.su_st_ap_materno,     ob.soc_st_descripcion, ob.soc_nr_peso,ob.soc_nr_calificacion 
FROM sed_objetivo ob 
JOIN sed_evaluacion ev  ON ob.se_evaluacion_pk = ev.se_evaluacion_pk
JOIN sed_usuarios us on  ev.su_colaborador_fk =     us.su_usuarios_pk)
);


来源:https://stackoverflow.com/questions/12740749/oracle-insert-multirows-from-subquery-with-more-than-one-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!