0

我需要根据另一个表更新一个表,所以我有以下语句:

update FAULTS
set FAULTS.FAULT_STATE = (
    select INCIDENTS.TICKET_STATE from INCIDENTS
    where INCIDENTS.CF_ID = FAULTS.CF_ID
)

它给了我错误说:

ORA-01427: single-row subquery returns more than one row

我尝试了 DISTINCT,但状态值也不同,所以 DISTINCT 不起作用。所以我也改了:

update FAULTS
set FAULTS.FAULT_STATE = (
    select INCIDENTS.TICKET_STATE from INCIDENTS
    where INCIDENTS.CF_ID = FAULTS.CF_ID and rownum = 1
)

现在它不会给出错误。

问题是,如果子查询返回null,它也会更新行并清除内容。有什么解决办法吗?

4

2 回答 2

1

您可以确保只更新匹配的行,如下所示:

update FAULTS
set FAULTS.FAULT_STATE = (
    select INCIDENTS.TICKET_STATE from INCIDENTS
    where INCIDENTS.CF_ID = FAULTS.CF_ID and rownum = 1
)
where exists (
    select INCIDENTS.TICKET_STATE from INCIDENTS
    where INCIDENTS.CF_ID = FAULTS.CF_ID
)
于 2012-11-13T22:45:05.723 回答
0

合并变体(10g+):

merge into faults
using (select cf_id, ticket_state from incidents) incidents
   on (incidents.cf_id = faults.cf_id)
when matched then update
 set fault_state=ticket_state;
于 2012-11-14T00:18:22.743 回答