i have a process to move rows from one database to another. Because of some circular foreign key reference chains i cannot remove the rows from the old database, nor can i insert them into the new database.
Since the entire operation happens in a transaction1, i want SQL Server to ignore referential integrity failures until i call COMMIT TRANSACTION
.
For example2:
Table: Turboencabulators Table: Marselvanes
========================= =======================
PK TurboencabulatorID int /-> PK MarselvaneID int
^ MarselvanesID int --/ HasGrammeter bit
| PantametricFan varchar(50)
+------------------------------- TurboencabulatorID int
If i try to insert the turboencabulator in the new table, it will fail without the marselvane already being there. Reversing the order has the same problem.
When trying to remove the old rows, i cannot delete one until the other is deleted.
i've tried doing an n-phase system, where all rows are inserted with any columns that are under foreign key constraint set to null. Then i update all the inserted rows, placing the proper missing values. Then, in order to delete the source rows, i null off all columns affected by a FK, then delete the actual rows.3
What i'd really prefer is to just do my T-SQL operations, and have SQL Server not tell me until i try to call commit.
Notes
1distributed
2contrived hypothetical
3which i'm not doing anymore