bulk insert is ignoring foreign key constraint

  • I have a bulk insert that worked great until I added a foreign key constraint on the table I am bulk inserting to.  I did this to make sure that a field called key code had to match something in a different table or else the insert would fail.  But the bulk insert still worked.  The constraint is set up on the proper field,  to enforce relationship for replication, to enforce relationship on inserts and updates, to cascade update and cascade delete. 

     

    can anyone help me out.  Thanks in advance BP

     

  • Bulk insert will by default ignore constraints and triggers. If you want to check triggers and constraints when using bulk insert then you have to specify in a with clause FIRE_TRIGGERS and CHECK_CONSTRAINTS e.g.

    BULK INSERT Northwind.dbo.[Order Details]

    FROM 'f:\orders\lineitem.tbl'

    WITH

    (

    FIELDTERMINATOR = '|',

    ROWTERMINATOR = ':\n',

    CHECK_CONSTRAINTS,

    FIRE_TRIGGERS

    )

    For more info see BOL

    Dave

  • thanks, oddly enough I found the answer not 5 seconds before you responded.  Thank you David for your quick response.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply