bcp aborts on first FK violation

  • I want to use BCP to load data from a text file.

    By default, constraints are turned off in bcp, so I use the CHECK_CONSTRAINTS hint.

    bcp aborts if ANY of the rows contains a FK violation. No data get loaded.

    So if I add the -b 1 batch size option, it loads all data UNTIL the first FK violation, but nothing after that.

    I want to load EVERYTHING ... except for the violations. But bcp won't let me. Is there a way?

  • I'm not sure if bcp would allow you to do that. However, you could load all data to a staging table and insert clean data into your final table to avoid PK violations.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • bruce.malmat (9/26/2013)


    I want to use BCP to load data from a text file.

    By default, constraints are turned off in bcp, so I use the CHECK_CONSTRAINTS hint.

    bcp aborts if ANY of the rows contains a FK violation. No data get loaded.

    So if I add the -b 1 batch size option, it loads all data UNTIL the first FK violation, but nothing after that.

    I want to load EVERYTHING ... except for the violations. But bcp won't let me. Is there a way?

    Yes, there is. If you take a look in Books Online for the BCP command, you'll find that the -m parameter changes the number of allowable errors. You can also use the -e parameter to identify a file where the error rows can be stored for validation and rework. Set the batch size back to at least 50,000, set -m to a very large number (like 2 billion), and let 'er rip.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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