Reputation: 55
I try to perform a merge, however, it does not allow me, I already add DISTINCT, purge the fields for update and in the USING function they are unique fields, they are not repeated each one has different information, it is worth mentioning that to the table that I want to insert or update data already exist.
MERGE INTO B69_TBL_ALERTA_CONCENTRADO concentrado
USING (SELECT DISTINCT
archivo.id_archivo,archivo.rfc, archivo.nombre_contribuyente , archivo.situacion_contribuyente ,
archivo.oficio_global, archivo.publicacion_presuntos , archivo.publicacion_definitivos
FROM B69_CAT_ALERTA_ARCHIVO archivo
INNER JOIN TBL_TRANSACCIONES txn
ON txn.titular = archivo.nombre_contribuyente)v_using
ON (concentrado.nombre_contribuyente = v_using.nombre_contribuyente)
WHEN MATCHED THEN
UPDATE SET
concentrado.id_archivo = v_using.id_archivo,
concentrado.snapshot_date = DATE '2021-06-16',
concentrado.rfc = v_using.rfc,
concentrado.situacion_contribuyente = v_using.situacion_contribuyente,
concentrado.oficio_global = v_using.oficio_global,
concentrado.publicacion_presuntos = v_using.publicacion_presuntos,
concentrado.publicacion_definitivos = v_using.publicacion_definitivos,
concentrado.last_update = CURRENT_DATE,
concentrado.id_alertatipo = 2
WHEN NOT MATCHED THEN
INSERT (concentrado.id_concentrado,concentrado.id_archivo,concentrado.snapshot_date,concentrado.rfc,concentrado.nombre_contribuyente,
concentrado.situacion_contribuyente,concentrado.oficio_global,concentrado.publicacion_presuntos,
concentrado.publicacion_definitivos,concentrado.baja_logica,concentrado.last_update,concentrado.id_alertatipo)
VALUES (b69id_gralaut.nextval,v_using.id_archivo,DATE '2021-06-16',v_using.rfc,v_using.nombre_contribuyente,
v_using.situacion_contribuyente,v_using.oficio_global,v_using.publicacion_presuntos,
v_using.publicacion_definitivos,'0',CURRENT_DATE,'2');
COMMIT;
END STP_69B_ALERTA_EXTERNA;
And I get the following error:
Connecting to the Alerts database.
ORA-30926: unable to get stable rowset in source tables
ORA-06512: in "CDO_ALERTAS.STP_69B_ALERTA_EXTERNA", line 4
ORA-06512: online 2
The process is over.
Disconnecting from the Alerts database.
Upvotes: 1
Views: 338
Reputation: 21075
Your problem - the using
subquery produces duplicated rows in the key nombre_contribuyente
Check with
with v_using as (SELECT DISTINCT
archivo.id_archivo,archivo.rfc, archivo.nombre_contribuyente , archivo.situacion_contribuyente ,
archivo.oficio_global, archivo.publicacion_presuntos , archivo.publicacion_definitivos
FROM B69_CAT_ALERTA_ARCHIVO archivo
INNER JOIN TBL_TRANSACCIONES txn
ON txn.titular = archivo.nombre_contribuyente)
select nombre_contribuyente, count(*)
from v_using
group by nombre_contribuyente
order by 2 desc;
On the top of the result you see the keys that have count > 1
You must reformulated the using
subquery such that the join key of merge is unique - Distinct
is not enough, you must use e.g. GROUP BY
on the key.
Upvotes: 1