carnahantom
Technical User
I am working in a transactional SQL Server 2012 development environment where I am writing a script to "shrink" (remove excess records from) the large tables in backup copies of our Production database. The tables have record counts in the multi-million range. Referential is not a big issue for this process since it is in a development environment using test data. However, I do need to follow up afterwards and eliminate orphans.
To increase performance and keep from filling up the transaction log, I am using the following sequence of transaction:
[ol 1]
[li] Copy records to be retained from the original table to a new non-permanent table[/li]
[li] Disable FKs and Triggers[/li]
[li] Truncate the original table [/li]
[li] Insert records from the non-permanent table into the original table[/li]
[li] Drop the non-permanent table[/li]
[li] Re-enable FK's and Triggers[/li]
[/ol]
The problem is I end up with a lots of orphans (expected since I am bypassing DRI).
Question: is there a generic way that I can delete the orphans using a batch process?
The ideal would be for the process to find and delete all orphaned records in the database, but I would settle for feeding the process the names of the tables where I know there are orphans. What I am trying to avoid is writing queries for each specific table resulting in massive code writing.
Any suggestions would be greatly appreciated.
Tom
To increase performance and keep from filling up the transaction log, I am using the following sequence of transaction:
[ol 1]
[li] Copy records to be retained from the original table to a new non-permanent table[/li]
[li] Disable FKs and Triggers[/li]
[li] Truncate the original table [/li]
[li] Insert records from the non-permanent table into the original table[/li]
[li] Drop the non-permanent table[/li]
[li] Re-enable FK's and Triggers[/li]
[/ol]
The problem is I end up with a lots of orphans (expected since I am bypassing DRI).
Question: is there a generic way that I can delete the orphans using a batch process?
The ideal would be for the process to find and delete all orphaned records in the database, but I would settle for feeding the process the names of the tables where I know there are orphans. What I am trying to avoid is writing queries for each specific table resulting in massive code writing.
Any suggestions would be greatly appreciated.
Tom