ademas si quieres hacerlo por sql server te recomiendo este enlace mas actualizado
Primero eliminar los datos de aag20001
Delete from aag20001
where aaBrowseType = 0
aaBrowseType = 0 si no esta vinculada
aaBrowseType = 1 esta vinculada
Segundo eliminar los datos de aag20002
Delete from aag20002
where aaSubLedgerHdrID = 0
Estos registros están vinculados con los registros de aag20001 que no poseen una vinculación.
Lego ejecutar el siguiente query para sincronizar los aaRowID y de esta forma evitar el error
de analitical accounting por duplicidad de clave primaria.
declare @CMPANYID smallint,
@maxValue int
select@maxValue = 0
select distinct @CMPANYID = CMPANYID from DYNAMICS.dbo.SY01500 where INTERID = DB_NAME()
select @maxValue = max(aaGLHdrID) from AAG30000
if @maxValue > 0
update DYNAMICS..AAG00102 set aaRowID = @maxValue where aaTableID = 30000 and CMPANYID = @CMPANYID
select @maxValue = max(aaSubLedgerHdrID) from AAG20000
if @maxValue > 0
update DYNAMICS..AAG00102 set aaRowID = @maxValue where aaTableID = 20000 and CMPANYID = @CMPANYID
select @maxValue = max(aaGLWorkHdrID) from AAG10000
if @maxValue > 0
update DYNAMICS..AAG00102 set aaRowID = @maxValue where aaTableID = 10000 and CMPANYID = @CMPANYID
select @maxValue = max(aaTreeID) from AAG00600
if @maxValue > 0
update DYNAMICS..AAG00102 set aaRowID = @maxValue where aaTableID = 600 and CMPANYID = @CMPANYID
select @maxValue = max(aaTrxDimID) from AAG00400
if @maxValue > 0
update DYNAMICS..AAG00102 set aaRowID = @maxValue where aaTableID = 400 and CMPANYID = @CMPANYID
select @maxValue = max(aaTrxDimCodeID) from AAG00401
if @maxValue > 0
update DYNAMICS..AAG00102 set aaRowID = @maxValue where aaTableID = 401 and CMPANYID = @CMPANYID
select @maxValue = max(aaDateID) from AAG00500
if @maxValue > 0
update DYNAMICS..AAG00102 set aaRowID = @maxValue where aaTableID = 500 and CMPANYID = @CMPANYID
select @maxValue = max(aaAcctClassID) from AAG00201
if @maxValue > 0
update DYNAMICS..AAG00102 set aaRowID = @maxValue where aaTableID = 201 and CMPANYID = @CMPANYID
select @maxValue = max(aaBudgetTreeID) from AAG00900
if @maxValue > 0
update DYNAMICS..AAG00102 set aaRowID = @maxValue where aaTableID = 900 and CMPANYID = @CMPANYID
select @maxValue = max(aaBudgetID) from AAG00903
if @maxValue > 0
update DYNAMICS..AAG00102 set aaRowID = @maxValue where aaTableID = 903 and CMPANYID = @CMPANYID
set nocount off