当我执行此查询时,收到以下错误消息:
" Ora-01427单行子查询返回多行"
UPDATE event_registrations xta
SET (event_start_date,
event_end_date,
planned_hours,
user_status,
last_updated_date,
last_updated_by,
last_update_sec) =
(SELECT xcat.event_start_date,
CASE
WHEN xt.event_status = g_closed_s THEN xta.event_end_date
WHEN xt.event_status = g_open_s THEN xcat.event_end_date
END,
xcat.planned_hours,
CASE
WHEN xt.event_status = g_closed_s THEN g_status_done
WHEN xt.event_status = g_open_s THEN g_status_inprogress
END,
SYSDATE,
p_login_user,
to_number(TO_CHAR(SYSDATE, g_time_format))
FROM event_registrations_temp xcat
LEFT OUTER JOIN EVENTS xt ON xt.ext_system_id = xcat.alm_id
WHERE xcat.action = g_action_modification
AND xcat.error_flag = g_err_flag_not_processed
AND xcat.event_registration_id = xta.ext_event_registration_id)
WHERE xta.ext_event_registration_id IN
(SELECT event_registration_id
FROM event_registrations_temp
WHERE action = g_action_modification
AND error_flag = g_err_flag_not_processed);
我尝试使用IN条件修复它,但它确实无法正常工作。任何人都可以帮我这个。
答案 0 :(得分:3)
要返回更新值的子查询返回多个记录。所以要么
event_registrations_temp
每个action
+ error_flag
+ event_registration_id
或
events
有多个ext_system_id
。 检查event_registrations_temp
:
select action, error_flag, event_registration_id
from event_registrations_temp
group by action, error_flag, event_registration_id
having count(*) > 1;
检查events
:
select ext_system_id
from events
group by ext_system_id
having count(*) > 1;
一旦您知道这两个问题中的哪一个,请记下如何决定您想要更新的一条记录,并将此条件添加到子查询中。这可以是WHERE
子句中的其他条件或某些聚合。
答案 1 :(得分:1)
这被称为相关更新'和IN不是解决方案。 但是在更新中使用它之前,您需要先检查select语句。
UPDATE event_registrations xta
SET (event_start_date,
event_end_date,
planned_hours,
user_status,
last_updated_date,
last_updated_by,
last_update_sec) =
(SELECT xcat.event_start_date,
CASE
WHEN xt.event_status = g_closed_s THEN xta.event_end_date
WHEN xt.event_status = g_open_s THEN xcat.event_end_date
END,
xcat.planned_hours,
CASE
WHEN xt.event_status = g_closed_s THEN g_status_done
WHEN xt.event_status = g_open_s THEN g_status_inprogress
END,
SYSDATE,
p_login_user,
to_number(TO_CHAR(SYSDATE, g_time_format))
FROM event_registrations_temp xcat
LEFT OUTER JOIN EVENTS xt ON xt.ext_system_id = xcat.alm_id
WHERE xcat.action = g_action_modification
AND xcat.error_flag = g_err_flag_not_processed
AND xcat.event_registration_id = xta.ext_event_registration_id)
WHERE xta.ext_event_registration_id =xcat.event_registration_id)
where exists (SELECT 1
FROM event_registrations_temp xcat
WHERE xcat.event_registration_id = xta.ext_event_registration_id)
答案 2 :(得分:1)
这是一个简单的问题。检查event_registrations_temp是否可以包含每个操作多个记录或error_flag或event_registration_id
select action, error_flag, event_registration_id
from event_registrations_temp
group by action, error_flag, event_registration_id
having count(*) > 1;