如何从union语句中插入值列表(使用Oracle)?我有以下,但它不起作用:
INSERT INTO PS_Z_EXS_EMP_TBL (EMPLID) VALUES (
SELECT ee.emplid
FROM ps_employees ee
WHERE ee.empl_status IN ('A', 'L')
AND ee.emplid NOT IN (SELECT vc.emplid
FROM ps_vc_plan_mem vc
WHERE vc.vc_plan_id in ('PNC-RS','PNC-SO','PNC-ESPP'))
AND ee.emplid IN
(SELECT srv.emplid
FROM ps_Z_ADD_EMP_SRVDT srv
WHERE srv.emplid = ee.emplid
AND srv.z_serv_dt_type = 'ESP'
AND service_dt <= (SELECT asofdate FROM psasofdate))
UNION select EMPLID from ps_vc_plan_mem
where vc_plan_id in ('PNC-RS','PNC-SO','PNC-ESPP')
);
答案 0 :(得分:3)
您不需要values
。只需使用insert . . . select
:
INSERT INTO PS_Z_EXS_EMP_TBL(EMPLID)
SELECT ee.emplid
FROM ps_employees ee
WHERE ee.empl_status IN ('A', 'L') AND
ee.emplid NOT IN (SELECT vc.emplid
FROM ps_vc_plan_mem vc
WHERE vc.vc_plan_id in ('PNC-RS','PNC-SO','PNC-ESPP')
) AND
ee.emplid IN (SELECT srv.emplid
FROM ps_Z_ADD_EMP_SRVDT srv
WHERE srv.emplid = ee.emplid AND
srv.z_serv_dt_type = 'ESP'AND
service_dt <= (SELECT asofdate FROM psasofdate)
)
UNION
select EMPLID
from ps_vc_plan_mem
where vc_plan_id in ('PNC-RS','PNC-SO','PNC-ESPP');
答案 1 :(得分:0)
插入从其他表中选择的数据时,您不需要“值”。 尝试以下查询:
;with ins (emplid) as(
select ee.emplid
from ps_employees ee inner join ps_Z_Add_EMP_SRVDT srv
ON ee.emplid=srv.emplid and srv.z_serv_dt_type='ESP' and srv.service_dt <=(select asofdate from psasofdate)
where ee.empl_status in ('A','L')
and not exists (select 1 from ps_vc_plan_mem vc inner join ps_employees ee2
on ee2.emplid=vc.emplid)
union select emplid from ps_vc_plan_mem where vc_plan_id in ('PNC-RS','PNC-SO','PNC-ESPP')
)
insert into PS_Z_EXS_EMP_TBL (EMPLID)
select emplid from t