ORA-30926 significa che Oracle ha provato ad abbinare una riga di destinazione a più di una riga sorgente. La causa abituale è un DISTINCT che si presumeva garantisse l'unicità ma non lo fa.
Perché DISTINCT non basta
Rimuove i duplicati solo sull'intera lista selezionata. Aggiungere una seconda colonna alla select cambia cosa significa "duplicato":
CREATE TABLE #MyTable(IdToUpdate INT, LogSetIdToUpdateTo INT); INSERT INTO #MyTable VALUES (1,1), (1,2), (2,1), (3,1); SELECT DISTINCT IdToUpdate, LogSetIdToUpdateTo FROM #MyTable; IdToUpdate compare due volte per il valore 1, quindi il merge ha una sorgente ambigua. Demo dal vivo.
Trova prima i colpevoli
with cte AS ( select distinct nullLogSetId.Id as IdToUpdate, knownLogSetId.LogSetId as LogSetIdToUpdateTo from MyTable knownLogSetId join MyTable nullLogSetId on knownLogSetId.IdentifierType = nullLogSetId.IdentifierType and knownLogSetId.Identifier = nullLogSetId.Identifier where knownLogSetId.IdentifierType = 'DEF' and knownLogSetId.LogSetId >= 0 and nullLogSetId.LogSetId = -1 ) SELECT IdToUpdate, COUNT(*) AS c FROM cte GROUP BY IdToUpdate HAVING COUNT(*) > 1; Sostituisci DISTINCT con un aggregato
Un aggregato ti dà esattamente una riga per chiave, che è ciò di cui MERGE ha bisogno:
merge into MyTable using ( select nullLogSetId.Id as IdToUpdate, MAX(knownLogSetId.LogSetId) as LogSetIdToUpdateTo from MyTable knownLogSetId join MyTable nullLogSetId on knownLogSetId.IdentifierType = nullLogSetId.IdentifierType and knownLogSetId.Identifier = nullLogSetId.Identifier where knownLogSetId.IdentifierType = 'DEF' and knownLogSetId.LogSetId >= 0 and nullLogSetId.LogSetId = -1 group by nullLogSetId.Id ) on (Id = IdToUpdate) when matched then update set LogSetId = LogSetIdToUpdateTo; Se un massimo non è la regola che vuoi, decidi esplicitamente quale riga sorgente deve vincere ed esprimilo — MIN, un filtro con numerazione di riga, o una vera correzione dei dati. Sceglierne una in silenzio è il modo in cui ORA-30926 si trasforma in un bug di dati errati.