Oracle, insert multirows from subquery with more t

2019-05-28 12:07发布

问题:

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)
);