While trying to load a large amount of data into SQL Server I was having an issue trying to truncate tables prior to loading the data, and I was unable to due to some FK constraints. My initial research led me to scripting the removal and creation of the FK constraints, but then i wondered, what if they changed. What if the end user added more that I was unaware of? My search then led me to a blog post where the original author was able to pragmatically backup, drop and recreate the FK Constraints. I modified the script a little to check for the existence of the FK_Details table first before trying to create it.

Once the sql below has run once, the FK_Details table will be populated with the drop_script and create_script columns with the proper SQL commands to create and drop the FKs. Im going to add a time stamp column to track when this was ran. This allows me to add this as part of my maintenance schedule, and track when things are done to the database.

If you want to actually execute on the drop or create, you’ll need to execute EXEC sp_executesql @drop then load your data, and re apply the FKs with EXEC sp_executesql @create;

All credit goes to the original author <- blog post here