When/how are Constraints Checked in a SqlBulkCopy w/ Check Constraints enabled?

sqlbulkcopy sql-server transactions

Question

I am performing several SqlBulkCopy's in a single transaction, I need to be able to roll back easily if anything goes wrong. I'm bulk copying several tables that have foreign keys to each other. I want these constraints checked. I'm copying the parent table first, then the child tables, but I'm receiving foreign key constraint errors. Does SqlBulkCopy include the rows inserted in the transaction when checking constraints?

Popular Answer

By default constraints are not checked. Change the options of the SqlBulkCopy to check them during insertion.

Check constraints while data is being inserted. By default, constraints are not checked



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why