I recently had to solve this exact problem in SQL Server ā copying rows within the same tables (A ā A, B ā B) and making sure all foreign keys point to the newly inserted rows.
To handle it, I built a reusable migration tool that:
Recursively copies related records based on FK dependencies
Remaps all FK references to the new rows
Supports filters, value overrides, and dry-run mode
If anyone's interested in trying it out or giving feedback, Iād be happy to share it privately.