I need to update a table based on another table, so I have the statement:
update FAULTS
set FAULTS.FAULT_STATE = (
select INCIDENTS.TICKET_STATE from INCIDENTS
where INCIDENTS.CF_ID = FAULTS.CF_ID
)
And it gives me error saying:
ORA-01427: single-row subquery returns more than one row
I tried DISTINCT but the state values are different too so DISTINCT does not work. So I changed it too:
update FAULTS
set FAULTS.FAULT_STATE = (
select INCIDENTS.TICKET_STATE from INCIDENTS
where INCIDENTS.CF_ID = FAULTS.CF_ID and rownum = 1
)
Now it does not give errors.
The problem is, if the sub-query returns a null, it will also update the row and clear the content. Any fix for that?
You can ensure only rows with a match are updated like this: